cgtk3gtksourceview

gtkSourceView - usage of query-tooltip-text


I want to use the callback "query-tooltip-text" on my gtkSourceView. I connect the callback to the signal by using:

...
g_signal_connect(G_OBJECT(attributes), "query-tooltip-text", G_CALLBACK(on_lineMarkerTooltip_displayed), context->plainTextEditor_lineMarkers[lineNumber-1]->message);

This works fine .. whenever the tooltip should be displayed, my method is called. ...->message is a c-string which is in memory permanmently. Here my callback method:

gchar* on_lineMarkerTooltip_displayed(GtkSourceMarkAttributes *attributes, GtkSourceMark *mark, char* message)
{
    printf("message3: %s\n", message); // just to see what is going on
    return message;
}

The gtk3 source doc stats that I should control the lifetime of the string and free it when done, I think that should be fine.

However my callback fails with a double-free segfault after it is called the second time:

...
message3: bad hour
message3: 
message3: `�/EV
*** Error in `./cron-gui': double free or corruption (fasttop): 0x000056452fc70240 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x70bcb)[0x7f85670a9bcb]
/lib/x86_64-linux-gnu/libc.so.6(+0x76f96)[0x7f85670aff96]
/lib/x86_64-linux-gnu/libc.so.6(+0x7778e)[0x7f85670b078e]
/usr/lib/x86_64-linux-gnu/libgtk-3.so.0(+0x214447)[0x7f85694
...

So different than told in the official doc, it looks like gtk3 frees the memory of the string after usage. I tried to modify my callback accordingly like that:

gchar* on_lineMarkerTooltip_displayed(GtkSourceMarkAttributes *attributes, GtkSourceMark *mark, char* message)
{
    printf("message3: %s\n", message); // just to see what is going on
    return strdup(message);
}

Which works well, however I fear that my application could eat memory now. Do you know what is the correct usage of the query-tooltip-text callback ?


Solution

  • Ok, I checked the source-code of libgtksourceview: The string is freed by the library, the documenation for the signal-handler is just wrong.

    I filed a bug for the maintainers of the package here: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=857873

    So actually it is correct to use

    ...
    return strdup(message);
    ...