当我启动一个新线程时为什么Valgrind会出现段错误

jac*_*row 9 c++ multithreading valgrind segmentation-fault

我正在用C++编写一个程序,我注意到一些非常奇怪的东西.

当我在Xcode下运行程序时,一切正常,但是当我在Valgrind下执行时,它会segmentation fault在几秒钟后给我一个.

我设法提取了一个非常简单的代码,它给了我这个错误:

#include <thread>

void exec_1() {}

int main(int argc, const char * argv[]) {

    std::thread simulator_thread;
    simulator_thread = std::thread(exec_1);
    simulator_thread.join();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我正在做的只是使用这些标志在Xcode下构建我的可执行文件:

CFLAGS:

-I/usr/local/lib/python3.6/site-packages/numpy/core/include
-I/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/include/python3.6m 
-Wno-unused-result -Wsign-compare -Wunreachable-code
-fno-common -dynamic -DNDEBUG -g -fwrapv -Wall -Wstrict-prototypes
Run Code Online (Sandbox Code Playgroud)

LDFLAGS:

-L/usr/local/opt/python3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m-darwin
-lpython3.6m -ldl -framework CoreFoundation
Run Code Online (Sandbox Code Playgroud)

然后在Valgrind下运行可执行文件以查找内存泄漏.你会看到我正在调用它,Python C API因为我在我的main代码中使用它但是这段代码却抛弃了我segfault而不使用它们.

无论如何Valgrind,以及其他一些东西,给我以下输出:

Thread 2:
==41660== Invalid read of size 4
==41660==    at 0x1016FA899: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib)
==41660==    by 0x1016FA886: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib)
==41660==    by 0x1016FA08C: thread_start (in /usr/lib/system/libsystem_pthread.dylib)
==41660==  Address 0x18 is not stack'd, malloc'd or (recently) free'd
==41660== 
==41660== 
==41660== Process terminating with default action of signal 11 (SIGSEGV)
==41660==  Access not within mapped region at address 0x18
==41660==    at 0x1016FA899: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib)
==41660==    by 0x1016FA886: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib)
==41660==    by 0x1016FA08C: thread_start (in /usr/lib/system/libsystem_pthread.dylib)
==41660==  If you believe this happened as a result of a stack
==41660==  overflow in your program's main thread (unlikely but
==41660==  possible), you can try to increase the size of the
==41660==  main thread stack using the --main-stacksize= flag.
==41660==  The main thread stack size used in this run was 8388608.
--41660:0:schedule VG_(sema_down): read returned -4
Run Code Online (Sandbox Code Playgroud)

是否可能在Valgrind下生成一个线程是导致错误的原因?

PS:
我的操作系统是MacOS 10.12.5,我正在使用Xcode 8.3.3Valgrind 3.13.0.

Dre*_*nis 8

是否可能在Valgrind下生成一个线程是导致错误的原因?

在运行使用pthread的二进制文件时,这确实是Mac OS X上Valgrind的问题:

不在_pthread_find_thread(OS X 10.11)中的映射区域内访问 https://bugs.kde.org/show_bug.cgi?id=349128

您在Valgrind中的失败与此处报告的相似:

在Valgrind下的Mac OS上的std :: thread.join()SIGSEGV