在以下来自核心转储的回溯中,A2:~A2被调用了两次:
#0 0x086f5371 in B1::~B1 (this=0xe6d3a030,
__in_chrg=<value optimized out>)
at /fullpath/b1.cpp:400
#1 0x086ffd43 in ~B2 (this=0xe6d3a030,
__in_chrg=<value optimized out>)
at /fullpath/b2.h:21
#2 B2::~B2 (this=0xe6d3a030,
__in_chrg=<value optimized out>)
at /fullpath/b2.h:21
#3 0x086ea516 in A1::~A1 (this=0xe3e93958,
__in_chrg=<value optimized out>)
at /fullpath/a1.cpp:716
#4 0x0889b85d in A2::~A2 (this=0xe3e93958,
__in_chrg=<value optimized out>)
at /fullpath/a2.cpp:216
#5 0x0889b893 in A2::~A2 (this=0xe3e93958,
__in_chrg=<value optimized out>)
at /fullpath/a2.cpp:216
#6 0x0862c0f1 in E::Identify (this=0xe8083e20, t=PT_UNKNOWN)
at /fullpath/e.cpp:713
A2是由A1派生而来,B2是由B1派生而来。只有B2有一个默认析构函数,所有基类的析构函数都是虚的。
代码看起来像这样:
e.cpp:
E::E(){
//... some code ...
myA1= new A2();
}
void E::Identify(){
//...
if(myA1){
delete myA1; //line 713 of e.cpp
myA1 = NULL;
}
}
a2.cpp:
A2::~A2(){
//...
if (sd) //sd is not null here and also not made null after deletion
{
delete [] sd; //when called the second time shouldn't it crash here?
}
//...
} // line 216 of a2.cpp
a1.cpp
A1::A1(){
//...
myB1 = new B2();
//...
}
A1::~A1(){
//...
delete myB1; //line 716 of a1.cpp
//...
}
我无法理解为什么对于同一对象A2,A2::~A2会被调用两次(回溯中this指针在第4和第5个帧中具有相同的值)。
如果我进入第4个帧并进行反汇编,它会打印与第5个帧反汇编代码非常不同的结果(大约90行汇编代码与大约20行汇编代码)。
A1 *myA1 = new A2();
是在构造函数中定义的局部变量,因此你无法在void E::Identify()
中访问它,除非你将其作为参数传递(但你没有这样做)。你确定这是你的原始代码吗?你展示的代码和你提出的问题之间存在差异。 - Alok Savenm
查找 ~A2 符号来查看这一点。 - PlasmaHH