- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在用 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;
}
我所做的只是使用这些标志在 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 下生成线程是错误的原因?
附言:
我的操作系统是 MacOS 10.12.5
,我使用的是 Xcode 8.3.3
和 Valgrind 3.13.0
。
最佳答案
Is it possible that spawning a thread under Valgrind is the cause of the error?
看来这确实是 Mac OS X 上运行使用 pthread 的二进制文件时 Valgrind 的问题:
在 _pthread_find_thread (OS X 10.11) 中访问不在映射区域内 https://bugs.kde.org/show_bug.cgi?id=349128
您在 Valgrind 上的失败与此处报告的类似:
关于c++ - 为什么在我启动新线程时 Valgrind 会出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44990380/
我是一名优秀的程序员,十分优秀!