当我在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;
最佳答案
原来这是glib-2.0.vapi中缺少的注释
在join()上方添加[DestroysInstance]可解决该问题。