成功调用析构函数或调用空析构函数后出现段错误。 C++

发布于 2024-10-23 13:35:10 字数 878 浏览 6 评论 0原文

非常抱歉,我无法提供我的代码的更多详细信息,因为我正在接管另一个项目。类结构非常复杂,我无法使用简单的示例重现该问题。

本质上,如果我删除一个对象,析构函数中的所有语句都会成功执行,但是一旦析构函数完成执行,就会发生段错误。即使我只是将析构函数清空而不执行任何操作,段错误仍然会发生。该类没有任何基类。

我的代码如下所示:

ParallelSynthesizer* p = new ParallelSynthesizer(argc, argv);
p->synthesize();
delete p;
cout << "after deleting" << endl; 

“删除后”未显示,因为段错误发生在此之前。但是p的析构函数执行成功。

[编辑后的一些评论]“synthesize()”方法确实使用多线程,但它非常简单:

pthread_t threads[num_threads];
// makes the "params" array here. skipped. 
for (int i=0; i<num_threads; i++) {
    pthread_create(&threads[i], NULL, synthesizeThreadMethod, (void*)(params[i]));
}

for (int i=0; i<num_threads; i++) {
    pthread_join(threads[i], NULL);;
}

这几乎全部都在 Synthesize() 方法中,所以我认为多线程不会导致任何问题。

我在 Linux 上使用 g++。有谁知道这个问题的可能原因?

对于无法找到产生此错误的简单示例,我再次表示歉意。

I am very sorry that I am not able to provide more details of my code, since I am taking over another project. The class structures are very complicated and I am unable to reproduce the issue using an easy example.

Essentally if I delete an object, all the statements in the destructor was executed successfully, but as soon as the destructor finishes execution, seg fault happens. Even if I just make the destructor empty and not do anything, the seg fault still happens. This class does not have any base class.

My code looks like this:

ParallelSynthesizer* p = new ParallelSynthesizer(argc, argv);
p->synthesize();
delete p;
cout << "after deleting" << endl; 

"after deleting" was not shown, as the seg fault happens before that. But the destructor of p is executed successfully.

[EDITED AFTER SOME COMMENTS] the "synthesize()" method does use multithreading, but it is very straightforward:

pthread_t threads[num_threads];
// makes the "params" array here. skipped. 
for (int i=0; i<num_threads; i++) {
    pthread_create(&threads[i], NULL, synthesizeThreadMethod, (void*)(params[i]));
}

for (int i=0; i<num_threads; i++) {
    pthread_join(threads[i], NULL);;
}

This pretty much all in the synthesize() method, so I don't think multithreading will result in any issue.

I am using g++ on linux. Does anybody know the possible causes of this problem?

I apologize again for not being able to find an easy example that produces this error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丿*梦醉红颜 2024-10-30 13:35:10

一个可能的原因是另一个对象在删除后尝试访问p

更新 您可以尝试通过 valgrind 运行您的代码。取决于您事先隔离问题的能力。到目前为止,我的猜测是您在类中做了一些不好的事情(例如构造一个对象并将 p 作为参数传递给它)。

One possible cause is that another object tries to access p after it got deleted.

Update You could try and run your code through valgrind. Depends a little on how well you can isolate the problem before hand. My guess so far would be that you do something bad inside your class (like constructing an object and passing p as parameter to it).

扛刀软妹 2024-10-30 13:35:10

根据你所说的很难说,但听起来你有一些堆损坏。

这类问题很难追踪,而且考虑到代码库很大,Stack Overflow 读者几乎不可能为您解决这个问题。我建议运行像 valgrind 这样的工具,它将跟踪内存访问并提示您哪里出了问题。

It's hard to say based on what you've said, but it sounds like you've got some heap corruption.

This kind of problem is tricky to trace and it's virtually impossible for Stack Overflow readers to fix this for you given a large code base. I would recommend running a tool like valgrind, which will track memory accesses and give you a hint at where things went wrong.

戏舞 2024-10-30 13:35:10

我猜测崩溃发生在 operator delete(void*) 期间,该操作是在析构函数之后由 delete p; 调用的。

有很多可能的原因会导致堆混乱,从而导致崩溃。一个常见的情况是,一些代码先前在new-ed对象之前或之后写入内存。我会在 valgrind memcheck 下运行该程序;它是一个非常有用的工具,专门用于追踪此类错误。

I would guess the crash happens during operator delete(void*), which is invoked by delete p; right after the destructor.

There are lots of possible causes for messing up the heap in a way that could cause a crash. A common one would be that some code previously wrote to memory before or after a new-ed object. I would run the program under valgrind memcheck; it's a very useful tool specifically for tracing down this sort of error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文