当我在 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] 解决了这个问题。
关于multithreading - Vala:当加入的线程未被引用时读取无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20517359/