***检测到glibc***/parent_child_conscall:双重空闲或损坏(fasttop):0x09d9
*** glibc detected *** ./parent_child_conscall: double free or corruption (fasttop): 0x09d9e028 ***
我的代码包含两个类。
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字节内存
我必须发布回溯报告吗?
任何帮助都是感谢。
这里的问题是创建points
类的两个实例。一个是带有points()
的临时的,当然还有一个带有x1
的声明。
执行x1 = points()
时,使用points()
构造创建一个临时对象,然后将其复制指定给x1
,然后销毁该临时对象。由于您没有提供复制赋值运算符,编译器将为您创建一个,但它只是复制指针,而不分配新的内存。这意味着您暂时有两个对象包含指向同一内存的指针。当临时对象被销毁时,您可以释放析构函数中的内存,使x1
中的指针悬空。当x1
被破坏时,您试图删除这个悬空指针,导致双自由错误。
解决这个问题的最佳方法是实现一个复制赋值运算符,最好还有一个复制构造函数,并在其中分配新内存和复制数据。
相关文章:
- 为什么会发生堆损坏
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 平均图像时图像损坏
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 为什么C中的通用链表中存储的数据已损坏
- gdb错误:Backtrace已停止:上一帧与此帧相同(堆栈已损坏?)
- C++双重释放或损坏(out)
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- 使用全局声明的向量时,C++双重释放错误/损坏
- 变量周围的堆栈'...'已损坏
- 运行时检查失败 #2 变量"A"周围的堆栈已损坏
- 检测到堆损坏:在正常块 c++ 动态 2D 数组之后
- 删除字符串后C++检测到堆损坏
- 双自由或损坏(fasttop):0x000000000063d070***c++sive程序
- c++双自由或损坏(fasttop)--不确定错误在哪里
- ***检测到glibc***/parent_child_conscall:双重空闲或损坏(fasttop):0x09d9
- 如何避免拷贝赋值操作符的双重自由或损坏(fasttop)
- 错误:双重自由或损坏(fasttop) -由析构函数引起,但如何
- c++:双重自由或损坏(fasttop)
- 在执行并行版本时,双重免费或损坏(fasttop)