- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在调用 pthread_join()
时遇到了一个 C++ 无法轻易重现的 SEGV(大约每 100,000 次测试运行中就会出现一次),因为我的应用程序正在关闭。我检查了 errno 的值,它是零。这是在 Centos v4 上运行的。
在什么情况下 pthread_join()
会得到一个 SEGV?这可能是某种竞争条件,因为它非常罕见。有人建议我不应该调用 pthread_detach() 和 pthread_exit(),但我不清楚原因。
我的第一个工作假设是 pthread_join()
被调用,而 pthread_exit()
仍在另一个线程中运行,这不知何故导致了 SEGV,但是许多人表示这不是问题。
在应用程序退出期间在主线程中获取 SEGV 的失败代码大致如下所示(为简洁起见省略了错误返回代码检查):
// During application startup, this function is called to create the child thread:
return_val = pthread_create(&_threadId, &attr,
(void *(*)(void *))initialize,
(void *)this);
// Apparently this next line is the issue:
return_val = pthread_detach(_threadId);
// Later during exit the following code is executed in the main thread:
// This main thread waits for the child thread exit request to finish:
// Release condition so child thread will exit:
releaseCond(mtx(), startCond(), &startCount);
// Wait until the child thread is done exiting so we don't delete memory it is
// using while it is shutting down.
waitOnCond(mtx(), endCond(), &endCount, 0);
// The above wait completes at the point that the child thread is about
// to call pthread_exit().
// It is unspecified whether a thread that has exited but remains unjoined
// counts against {PTHREAD_THREADS_MAX}, hence we must do pthread_join() to
// avoid possibly leaking the threads we destroy.
pthread_join(_threadId, NULL); // SEGV in here!!!
在退出时加入的子线程运行以下代码,该代码从上面在主线程中调用 releaseCond()
的点开始:
// Wait for main thread to tell us to exit:
waitOnCond(mtx(), startCond(), &startCount);
// Tell the main thread we are done so it will do pthread_join():
releaseCond(mtx(), endCond(), &endCount);
// At this point the main thread could call pthread_join() while we
// call pthread_exit().
pthread_exit(NULL);
该线程似乎正常启动,在应用程序启动期间创建它的过程中没有产生错误代码,并且线程正确地执行了它的任务,这在应用程序退出前花费了大约五秒钟。
什么可能导致这种罕见的 SEGV 发生以及我应该如何针对它进行防御性编程。一种说法是我对 pthread_detach() 的调用是问题所在,如果是这样,我的代码应该如何更正。
最佳答案
假设:
pthread_create
返回零(您正在检查它,对吧?)attr
是一个有效的 pthread_attr_t
对象(您如何创建它?为什么不直接传递 NULL 呢?)attr
没有指定线程是detached创建的pthread_detach
或 pthread_join
...那么 pthread_join
是“不可能”失败的,并且您可能遇到其他内存损坏或运行时中的错误。
[更新]
pthread_detach
的基本原理部分说:
The *pthread_join*() or *pthread_detach*() functions should eventually be called for every thread that is created so that storage associated with the thread may be reclaimed.
虽然它没有说它们是相互排斥的,pthread_join
documentation指定:
The behavior is undefined if the value specified by the thread argument to *pthread_join*() does not refer to a joinable thread.
我很难找到说分离线程不可连接的确切措辞,但我很确定这是真的。
因此,要么调用 pthread_join
要么调用 pthread_detach
,但不能同时调用。
关于c++ - 为什么在极少数情况下 pthread_exit() 在 pthread_detach() 之后调用时会导致 SEGV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11442601/
我在 _WebTryThreadLock 中偶尔会看到像这样的崩溃报告: Exception Type: SIGSEGV Exception Codes: SEGV_ACCERR a
vector *v =new vector[size]; for(i=0;i>(size/*, vector(1024)*/); 然后使用它 for(auto v& : vv) { v.pus
我有以下代码,它被 SEGV 信号杀死。使用调试器表明它被 main() 中的第一个 sem_init() 杀死。如果我注释掉第一个 sem_init() ,第二个会导致同样的问题。我试图弄清楚是什么
我目前正在调试我们的一个 C++ 应用程序中的段错误,这让我很难过,因为在发生段错误时没有生成核心文件。 在阅读和检查 ulimits 等之后,我可以重现核心文件未生成的情况。它似乎以某种方式与线程有
我在 strace 下运行了一个用汇编语言编写的简单程序,该程序仅执行 SYS_exit。 _start: mov rax, 0x3C mov rdi, 0x0 syscall
我在热门编码网站 codechef 上提交名为“Money Transformation”的问题时遇到问题。每次我提交解决方案时,我都会收到运行时错误名称 SIG SEGV。经过一些搜索在谷歌上我发现
Getting Segmentation error ,请在下面的c代码逻辑中帮助问题。程序因信号 SIGSEGV、段错误而终止。 /* * For your reference: * * Si
我有一个经过良好测试且没有崩溃的代码库(主要是 C++)。大多。代码的一部分——不可替代、难以维护或改进并链接到一个二进制库*——导致所有崩溃。这些不会经常发生,但一旦发生,整个程序就会崩溃。
我已经安装了一个处理程序(例如,crashHandler()),它具有一些文件输出功能。它是一个使用 crashHandler() 注册 SIGSEGV 的 linux 线程。需要写入文件,因为它将堆
我在 ubuntu 11.10 上安装了 vim-gnome 并且可以正常工作。使用 vundle 并安装了 seeral 软件包,一切正常。 我已经安装了 command-t,但在编译 C 扩展之前
我正在尝试编写一些高性能汇编函数作为练习,并且在运行程序时遇到了一个奇怪的段错误,但在 valgrind 或 nemiver 中没有。 基本上不应该运行的 cmov,带有越界地址,即使条件始终为假,也
我试图让 ASAN 使用一个程序,但我所做的任何事情都会导致 ASAN:DEADLYSIGNAL ,所以我尝试缩小范围并使用只有几个编译器选项的小型测试程序,只是为了看看它是否可以工作: $ cat
将 vim 与 pathogen 和 rails.vim 一起使用时,有时会导致 vim 崩溃。这是我的崩溃控制台日志文件。我怎样才能防止这种情况发生? Process: vim [1
我有一个应用程序可以扫描文件并收集有关文件的元数据。一个功能是获取文件的文件大小。为此,我使用了 winapi 函数 GetFileSizeEx(Handle, PLARGE_INTEGER) 。参数
我正在尝试调整结构的 vector 元素的大小,它会导致 segv。但是当我为一些小结构单独做它时它工作得很好。我很想知道它是如何将内存分配给结构的,其中有一个可以调整大小的 vector 元素。下面
试图在我的小电脑上构建一个矩阵,数组变大,内存容量用完,然后我得到了一个segv,我想知道C++中是否有一种方法可以在不跳出的情况下检测segv并然后继续? unsigned m = 10000; u
上下文化,在编译我的代码后,我收到了一个 SEGV 信号,这与未经授权的内存访问有关。考虑到源代码并且代码在最近的更改之前可以正常工作。为什么我会收到此信号? Note: Motif's type r
我收到错误 SEGV on unknown address 运行我的程序时。我很确定它来自 fgets() 但我不太清楚为什么。在此之前,我使用 scanf() 并且运行良好。输入很好地进入了数组,但
以下程序在使用 g++ 退出后出现段错误: #include #include
我正在构建一个包含大量 Android 源代码的库。如果为 Linux x86 编译,该库工作正常。我正在使用独立的工具链构建它。我之前在其他库中已经成功地做到了这一点,所以我觉得我的设置很好。无论如
我是一名优秀的程序员,十分优秀!