删除指向vector< int>的指针时出现段错误 [英] Seg fault when delete a pointer to vector<int>

查看:102
本文介绍了删除指向vector< int>的指针时出现段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个统计类,它有一个私有属性

''vector< int> * _x;''


统计数据的析构函数,我有这个代码来释放

内存:


统计数据::〜统计数据()

{


if(_x){

delete _x; //这里有段错...

}

}


但是我得到了一个Seg。我执行程序时出错。


你能告诉我我做错了什么吗?


谢谢。

(gdb)bt

#0 0x00248eaa in __gnu_cxx :: __ pool< true> :: _ M_reclaim_block()from

/usr/lib/libstdc++.so.6

#1 0x0250da84 in __gnu_cxx :: __ mt_alloc< int,

__gnu_cxx :: __ common_pool_policy< __ gnu_cxx :: __ pool,true> > :: deallocate

(这= 0x9107474,__ p = 0xdadadada,__ = 152471256)at mt_allocator.h:746

#2 0x0250daac in std :: _ Vector_base< int ,std :: allocator< int>

I have a class ''Statistics'' which has a private attribute
'' vector<int>* _x;''

And in the destructor of the Statistics, I have this code to free the
memory:

Statistics::~Statistics()
{

if (_x) {
delete _x; // Seg fault here...
}
}

But I get a Seg. Fault when I execute the program.

Can you please tell me what did I do wrong?

Thank you.
(gdb) bt
#0 0x00248eaa in __gnu_cxx::__pool<true>::_M_reclaim_block () from
/usr/lib/libstdc++.so.6
#1 0x0250da84 in __gnu_cxx::__mt_alloc<int,
__gnu_cxx::__common_pool_policy<__gnu_cxx::__pool, true> >::deallocate
(this=0x9107474, __p=0xdadadada, __n=152471256) at mt_allocator.h:746
#2 0x0250daac in std::_Vector_base<int, std::allocator<int>

:: _ M_deallocate(this = 0x9107474,__ p = 0x91686d8,__n = 0)at
::_M_deallocate (this=0x9107474, __p=0x91686d8, __n=0) at



stl_vector.h:123

#3 0x0250dacd在~_Vector_base(this = 0x91686d8)stl_vector.h:109

#4 0x0250db0b in~vector(this = 0x9107474)at stl_vector.h:273

#5 0x0250caf2 in~Statistics(this = 0xbfd85ad8)at

Statistics.cpp:54


stl_vector.h:123
#3 0x0250dacd in ~_Vector_base (this=0x91686d8) at stl_vector.h:109
#4 0x0250db0b in ~vector (this=0x9107474) at stl_vector.h:273
#5 0x0250caf2 in ~Statistics (this=0xbfd85ad8) at
Statistics.cpp:54

推荐答案

Piotr写道:
我有一个统计类,它有一个私有属性
''vector< int> * _x;''

在统计数据的析构函数中,我有这段代码来释放内存:

统计数据::〜统计数据()
{

if(_x){
删除_x; //这里有段错...
}
}

但是我得到了一个Seg。我执行程序时出错。

你能告诉我我做错了什么吗?


看起来_x在您尝试删除它时无效。

需要查看其余代码处理_x知道怎么回事
。你在设置吗?尝试转换?删除它

两次?

(gdb)bt
#0 0x00248eaa in __gnu_cxx :: __ pool< true> :: _ M_reclaim_block()from
/ usr / lib / libstdc ++。so.6
#1 0x0250da84 in __gnu_cxx :: __ mt_alloc< int,
__gnu_cxx :: __ common_pool_policy< __ gnu_cxx :: __ pool,true> > :: deallocate
(这= 0x9107474,__ p = 0xdadadada,__ = 152471256)at mt_allocator.h:746
I have a class ''Statistics'' which has a private attribute
'' vector<int>* _x;''

And in the destructor of the Statistics, I have this code to free the
memory:

Statistics::~Statistics()
{

if (_x) {
delete _x; // Seg fault here...
}
}

But I get a Seg. Fault when I execute the program.

Can you please tell me what did I do wrong?
Looks like _x isn''t valid at the point where you try to delete it.
Would need to see the rest of the code dealing with _x to know how that
happened. Are you setting it? Attempting conversions? Deleting it
twice?
(gdb) bt
#0 0x00248eaa in __gnu_cxx::__pool<true>::_M_reclaim_block () from
/usr/lib/libstdc++.so.6
#1 0x0250da84 in __gnu_cxx::__mt_alloc<int,
__gnu_cxx::__common_pool_policy<__gnu_cxx::__pool, true> >::deallocate
(this=0x9107474, __p=0xdadadada, __n=152471256) at mt_allocator.h:746




注意可疑的十六进制地址。 __p很可能是_x,

的值,我的猜测。使用您的调试器来追踪正在发生的事情。


Luke



Note the suspicious hex address. __p is likely to be the value of _x,
by my guess. Use your debugger to trace what''s going on.

Luke


谢谢。你可以解释一下'尝试转换

转换的意思吗?''


我把它设置在某个地方,否则,代码不会执行,对吧?


统计::〜统计()

{


if(_x){

删除_x; //这里有段错...

}


}

Thank you. Can you please explain what you meant by ''Attempting
conversions?''

And I have set it somewhere, otherwise, the code won''t execute, right?

Statistics::~Statistics()
{

if (_x) {
delete _x; // Seg fault here...
}

}


可以你能告诉我,如果

i有一个私有属性

''vector< int> * _x;''在我班级中,那么编写析构函数的正确方法是什么?


class statistic {

private:

vector< int> * _x;

};

Can you please tell me what is the right way to write the destructor if
i have a private attribute
of '' vector<int>* _x;'' in my class?

class Statistic {
private:
vector<int>* _x;
};


这篇关于删除指向vector&lt; int&gt;的指针时出现段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆