c ++ - 内存泄漏和多态

时间:2016-03-17 17:59:57

标签: c++ memory-leaks polymorphism

我一直试图用C ++中的多态性来解决问题从我的理解,它就像这样

class Base {
 //...
 public:
  virtual int Foo() {...} = 0;
};
//...
class Derived: public Base { //Could be protected or private as well
 //...
 public:
  int Foo() {...}
};

我也知道当我们有一个动态分配的对象数组时,在删除数组的每个单独条目后,我们必须在程序结束时调用delete [] arr。

因此,当我运行以下程序时,我不确定为什么会出现内存泄漏

#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;
}

编译时首先给我一个警告

poly.cc: In function 'int main()':
poly.cc:37:12: warning: deleting array 'Number* arr [10]' [enabled by default]

然后当我运行它时,它给了我这个

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

似乎正在成功添加和删除对象。我看不出有什么不对。

2 个答案:

答案 0 :(得分:6)

这里有两个问题。首先,这是无效的:

delete [] arr;

arr并非new[] - 所以它不需要delete[] - 编辑。 arr就在堆栈中。这就是你的编译器给你警告的原因! (直截了当地说错误)。好的经验法则是不要忽略编译器警告。

第二个问题在这里:

~Number() {
    cout << "Expression deleted" << endl;
}

当您delete Number时,~Number()将被执行,内存将被释放......但不会~Int()。如果您要删除基类指针,则还需要生成析构函数virtual。这应该也会引发警告,例如:

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];
              ^

答案 1 :(得分:1)

因为对于每个多态类,您需要定义一个虚拟析构函数。否则,通过指向基类的指针删除派生类将不会调用派生类析构函数。