处理删除命令期间出现分段错误
我有一个程序,它使用一个向量(称为 _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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据您提供的很少的信息,我假设您正在双重释放线程对象之一。您的问题不在于向量,而在于线程的生命周期管理。
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.如果您像这样分配 _stack :
您想删除它,并
注意分配数组时需要的删除后的 [] 。
if you allocated _stack like so:
you want to delete it with
note the [] after delete that's needed when you allocate an array.
假设 _libray[_currRunning] 包含一个指针,要么:
_library[_currRunning]
是无效地址_library[_currRunning]
已被删除如果是第二种情况,则 make确保在删除该元素后从向量中删除该元素(使用
erase
)。编辑:“无效”是指不是使用
new
创建的对象的地址。Assuming _libray[_currRunning] contains a pointer, either:
_library[_currRunning]
is an invalid address_library[_currRunning]
has already been deletedIf 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
.您不需要
删除
向量的任何对象 - 它会自行清理它们。您可能打算使用vector.erase()
来删除它。You don't need to
delete
any objects of a vector- it will clean them up itself. Likely, you meant to usevector.erase()
to remove it.除非您的
_currRunning
是在某个时刻用 new 创建的指针,否则删除将会产生影响。例如Unless your
_currRunning
is a pointer created at some point with new, delete will have repercussions. E.g.