【发布时间】:2015-04-29 12:13:24
【问题描述】:
class ParentClass {
protected:
int* intArray;
public:
~ParentClass(){
delete [] intArray;
}
};
class ChildClass : public ParentClass {
public:
ChildClass() : ParentClass() {
intArray = new int[5];
}
};
int main(int argc, const char * argv[]) {
ChildClass child;
child.~ChildClass(); //This line crashes the program. why??
}
它抛出的具体错误: 初始化(37640,0x7fff78623300)malloc:* 对象 0x100100aa0 的错误:未分配被释放的指针 * 在 malloc_error_break 中设置断点进行调试
指针正在引用ParentClass中声明的intArray,错误指出内存没有分配,但它是在ChildClass构造函数中分配的。
有人能解释一下产生这个错误的过程是什么吗?
【问题讨论】:
-
您确定是 that 行,而不是变量
child超出范围并使其析构函数 再次调用? -
是指定的行导致程序崩溃,还是该行的存在导致程序崩溃?
标签: c++