我想在我的 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
回调的正确用法是什么吗?