0

我的代码包含两个类。

  class points{
            char *p;
    public: points(){cout<<"constructor points called"<<endl;p=new char();}
            virtual ~points(){cout<<"destructor points called"<<endl;delete(p);}
  };
  class otherpoints: public points{
                    points x1;
    public: otherpoints(){cout<<"constructor otherpoints called"<<endl;x1=points();}
            ~otherpoints(){cout<<"destructor otherpoints called"<<endl;}
  };
  int main(int argc, char *argv[])
  {
    otherpoints y1;

    return 0;
  }

在这里,我在基类构造函数中分配一个指针,并在相应的基类析构函数中销毁指针的内存。

当我使用 valgrind 运行二进制文件时,它给出了错误:-

constructor points called

constructor points called

constructor otherpoints called

constructor points called

destructor points called

destructor otherpoints called

destructor points called

==2209== Invalid free() / delete / delete[]

==2209==    at 0x4024851: operator delete(void*) (vg_replace_malloc.c:387)

==2209==    by 0x8048A36: points::~points() (in home/santosh/programs/c++/parent_child_conscall)

==2209==    by 0x8048BB2: otherpoints::~otherpoints() (in /home/santosh/programs/c++/parent_child_conscall)

==2209==    by 0x804889A: main (in /home/santosh/programs/c++/parent_child_conscall)

==2209==  Address 0x42d5098 is 0 bytes inside a block of size 1 free'd

==2209==    at 0x4024851: operator delete(void*) (vg_replace_malloc.c:387)

==2209==    by 0x8048A36: points::~points() (in /home/santosh/programs/c++/parent_child_conscall)

==2209==    by 0x8048B32: otherpoints::otherpoints() (in /home/santosh/programs/c++/parent_child_conscall)

==2209==    by 0x8048889: main (in /home/santosh/programs/c++/parent_child_conscall)

==2209==    destructor points called

==2209== 

==2209== HEAP SUMMARY:

==2209==     in use at exit: 1 bytes in 1 blocks

==2209==   total heap usage: 3 allocs, 3 frees, 3 bytes allocated

==2209== 

==2209== LEAK SUMMARY:

==2209==    definitely lost: 1 bytes in 1 blocks

==2209==    indirectly lost: 0 bytes in 0 blocks

==2209==      possibly lost: 0 bytes in 0 blocks

==2209==    still reachable: 0 bytes in 0 blocks

==2209==         suppressed: 0 bytes in 0 blocks

==2209== Rerun with --leak-check=full to see details of leaked memory

==2209== 

==2209== For counts of detected and suppressed errors, rerun with: -v

==2209== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 18 from 7)

无法知道我无法释放哪个 1 字节内存。

我必须发布回溯报告吗?

任何帮助都是感激的。

4

1 回答 1

3

这里的问题是您创建了points该类的两个实例。一个是临时的points(),当然还有一个是声明的x1

When you do x1 = points() you create a temporary object with the points() construct, this is then copy-assigned to x1, and then the temporary object is destroyed. Since you do not provide a copy-assignment operator, the compiler will create one for you, but it simply copies the pointer, and don't allocate new memory. This means you for a while have two objects containing a pointer to the same memory. When the temporary object is destroyed you free the memory in the destructor, leaving the pointer in x1 dangling. When x1 is destroyed you try to delete this dangling pointer, leading to the double-free error.

The best way to solve this, is to implement a copy-assignment operator, and preferably a copy-constructor as well, and in those allocate new memory and copy the data.

于 2013-08-27T12:15:35.923 回答