Ad

C - SIGSEGV Whilst Trying To Append To GTKTextBuffer

I'm trying to write a function to append a given text string to the end of my GTKTextBuffer, however am having issues with what I assume to be memory allocation issues?

So, here is the function I have written to do the append:

void append_to_log(GtkTextBuffer *buffer, char* strToSave) {

    GtkTextIter *iter;

    gtk_text_buffer_get_end_iter(buffer, iter);

    gtk_text_buffer_insert(buffer, iter, strToSave, strlen(strToSave));

}

When I try to start my program, and add a "Logging started... \n" message using the function...

append_to_log(loggingBuffer, "Logging started... \n");

...I get this error:

/home/csc/CLionProjects/SDS-CW/GUIs/Server/cmake-build-debug/SDS-CW
*** stack smashing detected ***: <unknown> terminated

However, when I try manually adding the first line, it works fine:

gtk_text_buffer_set_text(loggingBuffer, "Logging started... \n", 20);

So I tried setting an inital line manually, then appending a new line afterwards...

gtk_text_buffer_set_text(loggingBuffer, "Logging started... \n", 20);
append_to_log(loggingBuffer, "Logging continued... \n");

... I get a Segmentation Fault.

Any ideas where I'm going wrong, and how I may be able to fix this?

Many thanks.

Ad

Answer

GtkTextIter iter;
gtk_text_buffer_get_end_iter(buffer, &iter);

You need to give pointer to existing GtkTextIter instance, and not pointer to nowhere, as in your code.

Ad
source: stackoverflow.com
Ad