Q:

Gtk-warning: Invalid text buffer iterator when writing to the same text view multiple times

This below function gets called every second and tries to write the total_successful_connects.
It works sometimes but I get the above error and program crashes.
gtk_text_buffer_set_text() will delete the previous buffer content. Does it not validate the iterator? 
If not, What should I do to validate the iterator?
void display_status()
{
    char output_str[100];

    sprintf(output_str, "%u", stats->total_successful_connects);
    gtk_text_buffer_set_text(
        config->text_buffer,
        output_str, strlen(output_str));

}

I have initialized config->textbuffer once during start up by calling gtk_text_view_get_buffer(my_text_view)
I am using gtk+ 2
Error in detail:
(gedit:7793): Gtk-WARNING **: Invalid text buffer iterator: either the iterator is uninitialized, or the characters/pixbufs/widgets in the buffer have been modified since the iterator was created.
You must use marks, character numbers, or line numbers to preserve a position across buffer modifications.
You can apply tags and insert marks without invalidating your iterators,
but any mutation that affects 'indexable' buffer contents (contents that can be referred to by character offset)
will invalidate all outstanding iterators

A:

You should use gdk_threads_add_idle() to schedule the call to gtk_text_buffer_set_text() in the main thread. Like this:
struct DispatchData {
    GtkTextBuffer *buffer;
    char *output_str;
};

static gboolean display_status_textbuffer(struct DispatchData *data)
{
    gtk_text_buffer_set_text(data->buffer, data->output_str, strlen(data->output_str));
    g_free(data);
    return G_SOURCE_REMOVE;
}

void display_status()
{
    struct DispatchData *data = g_new0(struct DispatchData, 1);
    data->output_str = g_strdup_printf("%u", stats->total_successful_connects);
    data->buffer = config->buffer;
    gdk_threads_add_idle(display_status_textbuffer, data);
}

(This is assuming that you do more non-GUI work in display_status() than just what you showed above; otherwise it would make sense to have just one every-second function that you initiate with gdk_threads_add_timeout().

