我正在使用C++编写程序,发现了一些奇怪的问题。当我在Xcode下运行程序时一切正常,但是在Valgrind下运行几秒钟后就会出现“段错误(segmentation fault)”。我已经提取了一个非常简单的代码,也会出现这个错误:
我是一个有用的助手,可以翻译文本。
我正在做的事情只是使用以下标志在Xcode下构建我的可执行文件:
CFLAGS:
LDFLAGS:
#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;
}
我是一个有用的助手,可以翻译文本。
我正在做的事情只是使用以下标志在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
LDFLAGS:
-L/usr/local/opt/python3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m-darwin
-lpython3.6m -ldl -framework CoreFoundation
然后在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
可能在Valgrind下创建线程是错误的原因吗?
P.S:
我的操作系统是MacOS 10.12.5
,我正在使用Xcode 8.3.3
和Valgrind 3.13.0
。
std::thread simulator_thread( exec_1);
- user2100815std::thread
数组并使用for
循环为它们分配真实对象来生成多个线程。在这里,我尝试尽可能地保持结构与我的主要代码接近。 - jackscorrow