c++;-内存泄漏与多态性 我一直在试图从我理解的C++中找出多态性,它是这样的< /P> class Base { //... public: virtual int Foo() {...} = 0; }; //... class Derived: public Base { //Could be protected or private as well //... public: int Foo() {...} };
我还知道,当我们有一个动态分配对象的数组时,我们必须在删除数组中的每个条目后,在程序结束时调用delete[]arr 因此,当我运行以下程序时,我不确定为什么会出现内存泄漏c++;-内存泄漏与多态性 我一直在试图从我理解的C++中找出多态性,它是这样的< /P> class Base { //... public: virtual int Foo() {...} = 0; }; //... class Derived: public Base { //Could be protected or private as well //... public: int Foo() {...} };,c++,memory-leaks,polymorphism,C++,Memory Leaks,Polymorphism,我还知道,当我们有一个动态分配对象的数组时,我们必须在删除数组中的每个条目后,在程序结束时调用delete[]arr 因此,当我运行以下程序时,我不确定为什么会出现内存泄漏 #include <iostream> using namespace std; class Number { public: ~Number() { cout << "Expression deleted" << endl; } virtual void print(
#include <iostream>
using namespace std;
class Number {
public:
~Number() {
cout << "Expression deleted" << endl;
}
virtual void print() = 0;
};
class Int: public Number {
private:
int num;
public:
Int(int n) {
num = n;
}
void print() {
cout << "Num: " << num << endl;
}
~Int() {
cout << "Number deleted" << endl;
}
};
int main() {
Number *arr[10];
arr[0] = new Int(1);
arr[1] = new Int(2);
arr[2] = new Int(3);
arr[0]->print();
arr[1]->print();
arr[2]->print();
delete arr[0];
delete arr[1];
delete arr[2];
delete [] arr;
}
然后当我运行它时,它会给我这个
Num: 1
Num: 2
Num: 3
Expression deleted
Expression deleted
Expression deleted
*** glibc detected *** ./a.out: munmap_chunk(): invalid pointer: 0x00007fff785732d0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7db26)[0x7f9274641b26]
./a.out[0x400b07]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f92745e576d]
./a.out[0x400959]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:32 43144247
00601000-00602000 r--p 00001000 00:32 43144247
00602000-00603000 rw-p 00002000 00:32 43144247
01b5f000-01b91000 rw-p 00000000 00:00 0 [heap]
7f92740b1000-7f92740c7000 r-xp 00000000 fc:00 50593804 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f92740c7000-7f92742c6000 ---p 00016000 fc:00 50593804 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f92742c6000-7f92742c7000 r--p 00015000 fc:00 50593804 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f92742c7000-7f92742c8000 rw-p 00016000 fc:00 50593804 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f92742c8000-7f92743c3000 r-xp 00000000 fc:00 50602224 /lib/x86_64-linux-gnu/libm-2.15.so
7f92743c3000-7f92745c2000 ---p 000fb000 fc:00 50602224 /lib/x86_64-linux-gnu/libm-2.15.so
7f92745c2000-7f92745c3000 r--p 000fa000 fc:00 50602224 /lib/x86_64-linux-gnu/libm-2.15.so
7f92745c3000-7f92745c4000 rw-p 000fb000 fc:00 50602224 /lib/x86_64-linux-gnu/libm-2.15.so
7f92745c4000-7f9274778000 r-xp 00000000 fc:00 50602238 /lib/x86_64-linux-gnu/libc-2.15.so
7f9274778000-7f9274977000 ---p 001b4000 fc:00 50602238 /lib/x86_64-linux-gnu/libc-2.15.so
7f9274977000-7f927497b000 r--p 001b3000 fc:00 50602238 /lib/x86_64-linux-gnu/libc-2.15.so
7f927497b000-7f927497d000 rw-p 001b7000 fc:00 50602238 /lib/x86_64-linux-gnu/libc-2.15.so
7f927497d000-7f9274982000 rw-p 00000000 00:00 0
7f9274982000-7f9274a84000 r-xp 00000000 fc:00 41962840 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f9274a84000-7f9274c83000 ---p 00102000 fc:00 41962840 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f9274c83000-7f9274c8b000 r--p 00101000 fc:00 41962840 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f9274c8b000-7f9274c8d000 rw-p 00109000 fc:00 41962840 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f9274c8d000-7f9274c90000 rw-p 00000000 00:00 0
7f9274c90000-7f9274cb2000 r-xp 00000000 fc:00 50602228 /lib/x86_64-linux-gnu/ld-2.15.so
7f9274e84000-7f9274e89000 rw-p 00000000 00:00 0
7f9274eae000-7f9274eb2000 rw-p 00000000 00:00 0
7f9274eb2000-7f9274eb3000 r--p 00022000 fc:00 50602228 /lib/x86_64-linux-gnu/ld-2.15.so
7f9274eb3000-7f9274eb5000 rw-p 00023000 fc:00 50602228 /lib/x86_64-linux-gnu/ld-2.15.so
7fff78553000-7fff78574000 rw-p 00000000 00:00 0 [stack]
7fff785c7000-7fff785c9000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted
似乎正在成功添加和删除对象。我看不出有什么问题。这里有两个问题。首先,这是无效的:
delete [] arr;
arr
不是new[]
-ed,因此它不需要delete[]
-ed。arr
只是在堆栈上。这就是为什么你的编译器给了你警告!(直截了当的错误)。好的经验法则是不要忽略编译器警告
第二个问题是:
~Number() {
cout << "Expression deleted" << endl;
}
因为对于每个polymoprhic类,都需要定义一个虚拟析构函数。否则,通过指向基类的指针删除派生类将不会调用派生类析构函数。谢谢。我没有得到你最后提到的那个错误。但是,我将析构函数设置为虚拟,并删除了delete[]arr。它现在可以工作了。只是一个后续问题。当我运行代码时,似乎首先调用Int析构函数,然后为数组中的每个对象调用Number析构函数。为什么析构函数必须调用两次?再次感谢。@Q_A什么都不叫两次。您看到的是派生子对象在基础子对象之前被销毁。。。一切都需要销毁。@Q_A另外,不要将答案编辑到你的问题中。您可以通过接受答案来指出对您有帮助的答案(请参阅)。
~Number() {
cout << "Expression deleted" << endl;
}
main.cpp: In function 'int main()':
main.cpp:37:14: warning: deleting object of abstract class type 'Number' which has non-virtual destructor will cause undefined behaviour [-Wdelete-non-virtual-dtor]
delete arr[0];
^