当我在 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 代码中的连接调用和 _g_thread_unref0 宏之间,无效读取在 valgrind 输出中消失了。
g_thread_join (thread);
result = 0;
thread = NULL;
_g_thread_unref0 (thread);
return result;