当我在valgrind中编译并运行下面的代码时,看起来当我加入线程时线程被释放,然后当它被取消引用时,一些已经被释放的内存被读取。
这是valgrind的“假阳性”吗?如果没有,在较大的并行程序中一般可以安全地忽略它吗?我该如何解决它?
int main (string[] args) {
Thread<int> thread = new Thread<int>.try ("ThreadName", () => {
stdout.printf ("Hello World");
return 0;
});
thread.join ();
return 0;
}
==2697== Invalid read of size 4
==2697== at 0x50F2350: g_thread_unref (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3800.1)
==2697== by 0x400A65: _vala_main (in /home/lockner/test)
==2697== by 0x400A9C: main (in /home/lockner/test)
==2697== Address 0x5dc17e8 is 24 bytes inside a block of size 72 free'd
==2697== at 0x4C2B60C: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2697== by 0x50F2547: g_thread_join (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3800.1)
==2697== by 0x400A4B: _vala_main (in /home/lockner/test)
==2697== by 0x400A9C: main (in /home/lockner/test)
当我手动添加“thread = NULL;”时在生成的C代码中,在join调用和_g_thread_unref0宏之间,valgrind输出中的无效读取消失了。
g_thread_join (thread);
result = 0;
thread = NULL;
_g_thread_unref0 (thread);
return result;
答案 0 :(得分:4)
事实证明,这是glib-2.0.vapi中缺少的注释
在join()上面添加[DestroysInstance]可以解决问题。
答案 1 :(得分:1)
问题是g_thread_join
已删除1个引用。所以生成的代码实现了双重免费。
如果您需要添加[DestroysInstance]
,这显然是valac / GThread
绑定中的错误。