开发者

Segmentation fault calling std::map::clear

I have been struggling with a segmentation fault for months, now I'm here to ask for help. The segmentation fault appears when I call the following function

void foo(..., std::map<MyClass*, double> & x) {
if ( !x.empty() ) x.clear();
...
}

Class A {
private:
map<MyClass*, double> _N;
public:
void f(...) {
foo(..., _N);
...
}
};

//in main routine,开发者_开发百科 the function is called in a loop
A a;
while(...) {
a.f(...);
}

Using gdb, I tacked the error to the line calling the clear() function, it shows "double free or corruption" error, and the program aborts at calling c++/4.1.2/ext/new_allocator.h:94 delete(__P) which further calls free() from the gnu library /lib64/libc.so.6. But since the elements in the map are not allocated by new, why it still calls free() to clear it up. I would really appreciate your comments. Thank you.


Given that the map is owned by another object it suspiciously sounds that the map-owning object was already deleted when the clear was called.

Also note that names starting with underscore and a capital letter are reserved for the implementation - you aren't allowed to use them.


The code looks fine to me. At least with the limited context you have provided. Usually when I run into issues like this I will simply run the valgrind memcheck tool to find the place were the first "delete" happened. Once you know that, these issues can be pretty simple to solve.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜