处理删除命令期间出现分段错误

发布于 2024-10-23 18:11:35 字数 875 浏览 1 评论 0原文

我有一个程序,它使用一个向量(称为 _library),该向量保存我创建的“线程”类的对象(保存一组数据,并在其构造函数中分配一些内容)。

现在,我尝试运行我的程序,调用这一行:

delete (_library[_currRunning]);

-> 并从我的编译器中收到可怕的分段错误消息。

我不明白这里的问题是什么,因为我执行了边界检查,而且 - 更令人惊讶的是:它适用于其他输入,当我之前测试过它时!

一般来说,使用“删除”时什么会导致分段错误,以及如何防止代码中出现此类错误?

此外,我还有一个“thread”类的析构函数,只有一行:

delete (_stack); 

其中 _stack 是我在 Ctor 中分​​配的 char*。

这是我的“线程”对象字段:

char* _stack;  
int _tid;  
void (*_thread_func)(void);  
sigjmp_buf _jbuf;  
Sync* _sync;  
int _status; 

在“线程”Ctor 中,(在其他线程之间)有这一行:

_stack = new char[STACK_SIZE]; 

这是它的 Dtor:

delete[] _stack;    

在我的大程序中,我有这样的声明:

vector<thread*> _library;  

使用“删除”是否有任何问题在我的析构函数中,而不是使用“free”?

I have a program that using a vector (called _library) that holds objects of the class 'thread' that I've created (holds a set of data, and allocating some stuff in its Constructor).

Now, I've tried to run my program, calling this line:

delete (_library[_currRunning]);

->and got the scary Segmentation fault message from my compiler.

I don't understand what is the problem here, since I perform boundary checks, and - what's more surprising: it works on other inputs, when I've tested it before!

In general, what can cause a segmentation fault when using 'delete', and how could I prevent such errors in my code?

In addition, I have a destructor for the 'thread' class, having this single line:

delete (_stack); 

where _stack is a char* that I've allocated in the Ctor.

Here're my 'thread' object fields:

char* _stack;  
int _tid;  
void (*_thread_func)(void);  
sigjmp_buf _jbuf;  
Sync* _sync;  
int _status; 

In 'thread' Ctor, there is (between others) this line:

_stack = new char[STACK_SIZE]; 

And this is its Dtor:

delete[] _stack;    

In my big program, i have this declaration:

vector<thread*> _library;  

Is there any problem of using 'delete' inside my Destructor, instead of using 'free'?

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

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

发布评论

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

评论(5

守不住的情 2024-10-30 18:11:35

根据您提供的很少的信息,我假设您正在双重释放线程对象之一。您的问题不在于向量,而在于线程的生命周期管理。

With the very little info you've provided I would assume that you are double freeing one of the thread objects. Your problem is not with vector but with your lifetime management of the threads.

黄昏下泛黄的笔记 2024-10-30 18:11:35

如果您像这样分配 _stack :

_stack = new char[SOME_LEN];

您想删除它,并

delete[] _stack;

注意分配数组时需要的删除后的 [] 。

if you allocated _stack like so:

_stack = new char[SOME_LEN];

you want to delete it with

delete[] _stack;

note the [] after delete that's needed when you allocate an array.

醉南桥 2024-10-30 18:11:35

假设 _libray[_currRunning] 包含一个指针,要么:

  1. _library[_currRunning] 是无效地址
  2. _library[_currRunning] 已被删除

如果是第二种情况,则 make确保在删除该元素后从向量中删除该元素(使用erase)。

编辑:“无效”是指不是使用new创建的对象的地址。

Assuming _libray[_currRunning] contains a pointer, either:

  1. _library[_currRunning] is an invalid address
  2. _library[_currRunning] has already been deleted

If it's the second case, make sure you remove the element from the vector (using erase) after you delete it.

EDIT: By "invalid", I mean the address to an object that wasn't created with new.

醉梦枕江山 2024-10-30 18:11:35

您不需要删除向量的任何对象 - 它会自行清理它们。您可能打算使用 vector.erase() 来删除它。

You don't need to delete any objects of a vector- it will clean them up itself. Likely, you meant to use vector.erase() to remove it.

祁梦 2024-10-30 18:11:35

除非您的 _currRunning 是在某个时刻用 new 创建的指针,否则删除将会产生影响。例如

void func()
{
   int a = 1;
   int* b = new int(2);
   delete b; // Ok, deleting a pointer
   delete a; // Can't delete non-pointer - should be a compilation error (?)
   delete &a; // This will call the destructor of a, but then the program 
              // will segfault when a goes out of scope at the end of this function.
}

Unless your _currRunning is a pointer created at some point with new, delete will have repercussions. E.g.

void func()
{
   int a = 1;
   int* b = new int(2);
   delete b; // Ok, deleting a pointer
   delete a; // Can't delete non-pointer - should be a compilation error (?)
   delete &a; // This will call the destructor of a, but then the program 
              // will segfault when a goes out of scope at the end of this function.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文