Hi you did find one of the memory leaks. I have re-worked your code to correct for the other memory leak and other problems, I have also done some changes which might improve performance for you.
- Used g_free on the string allocated by g_strdup_printf() when it is no longer needed. (the other memory leak)
- Replaced the use of GDK_Return (now deprecated) with GDK_KEY_Return.
- Reworked the code so that the return value is well defined and return TRUE for the handling of the return key. As we hand done handling all the work necessary.
- Reworked the code so that the line scanning is a bit quicker (scan once instead of three times)
- Reworked code to use a GString to build the return character and indenting string instead of doing multiple calls to gtk_text_buffer_insert(). Note each call to gtk_text_buffer_insert() will emit a "insert-text" signal
Code:
static const char indent_spaces[] = " ";
static gboolean txtinput_key_press_event(GtkWidget *widget, GdkEventKey *event)
{
GtkTextBuffer *buffer;
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txtinput[gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook))]));
GtkTextIter current_iter;
gtk_text_buffer_get_iter_at_mark(buffer,¤t_iter,gtk_text_buffer_get_insert(buffer));
int line, col, chars;
chars = gtk_text_iter_get_offset(¤t_iter);
line = gtk_text_iter_get_line(¤t_iter);
col = gtk_text_iter_get_line_offset(¤t_iter);
gchar *msg;
msg = g_strdup_printf("Char %d Line %d Col %d", chars +1, line +1, col +1);
gtk_statusbar_pop(GTK_STATUSBAR(statusbar), 0);
gtk_statusbar_push(GTK_STATUSBAR(statusbar), 0, msg);
g_free(msg);
if (gtk_source_view_get_auto_indent(GTK_SOURCE_VIEW(txtinput[gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook))])) == TRUE) {
gint indent_width;
indent_width = 4;/*gtk_source_view_get_indent_width(GTK_SOURCE_VIEW(txtinput[gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook))]));*/
GtkTextIter line_iter,next_line_iter;
int next_line,offset_end_of_line;
switch (event->keyval) {
case GDK_KEY_Return:
gtk_text_buffer_get_iter_at_line(buffer,&line_iter,line);
char *line_text;
line_text = gtk_text_buffer_get_text(buffer,&line_iter,¤t_iter,TRUE);
int spaces=0, count=0, i;
int count_open = 0, count_close = 0;
size_t line_text_len = strlen(line_text);
for (i=0; i < line_text_len; i++) {
if(line_text[i] == ' ')
spaces++;
else
break;
}
for ( ; i < line_text_len; i++) {
if (line_text[i]==':')
count++;
if(line_text[i]=='(')
count_open++;
if(line_text[i]==')')
count_close++;
}
GString * ispaces = g_string_new("\n");
if (count==1) {
int j;
for (j=0; j<spaces/indent_width + 1; j++) {
g_string_append_len(ispaces, indent_spaces, indent_width);
}
} else {
int j;
for (j=0; j<spaces/indent_width; j++) {
g_string_append_len(ispaces, indent_spaces, indent_width);
}
}
/*Problem with this code, check for line wrap as this can add great length to the line*/
if(count_open>count_close) {
int k,j;
for(k=0; k<count_open-count_close; k++) {
for (j=0; j<spaces/indent_width + 1; j++) {
g_string_append_len(ispaces, indent_spaces, indent_width);
}
}
}
/******************************************************/
gtk_text_buffer_insert(buffer, ¤t_iter, ispaces->str, ispaces->len);
g_string_free(ispaces, TRUE);
g_free(line_text);
return TRUE;
default:
return FALSE;
}
}
return FALSE;
}
Hope this helps and that by using GLib features it can make code easier to read.