0

我想在我的 gtkSourceView 上使用回调“query-tooltip-text”。我使用以下方法将回调连接到信号:

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

这很好用.. 每当显示工具提示时,都会调用我的方法。...->message是一个永久保存在内存中的 c 字符串。这是我的回调方法:

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

gtk3 源文档统计我应该控制字符串的生命周期并在完成后释放它,我认为应该没问题。

但是,我的回调在第二次调用后因双自由段错误而失败:

...
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
...

与官方文档中的说法完全不同,看起来 gtk3 在使用后释放了字符串的内存。我试图像这样修改我的回调:

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);
}

效果很好,但是我担心我的应用程序现在会吃掉内存。你知道query-tooltip-text回调的正确用法是什么吗?

4

1 回答 1

0

好的,我检查了 libgtksourceview 的源代码:字符串被库释放,信号处理程序的文档是错误的。

我在这里为软件包的维护者提交了一个错误: https ://bugs.debian.org/cgi-bin/bugreport.cgi?bug=857873

所以实际上使用是正确的

...
return strdup(message);
...
于 2017-03-15T21:39:20.023 回答