析构函数的 C++ 问题
C++ Problems with destructor
我有一个问题,我不知道解决方法,需要你的帮助!提前致谢!
这里是我的class(class复制不完整)
#ifndef _CGRAPH_H_
#define _CGRAPH_H_
class Cgraph{
typedef std::list<int> vlist;
typedef std::map<int, int > vidmap;
typedef std::map<int, std::string > vutfmap;
typedef std::map<int, vlist > vmap;
typedef std::map<int, vlist >::iterator vmap_it;
int frequency;
private:
vidmap vertices;
vutfmap utf;
vmap wattackedges; // label 0
vmap battackedges; // label 1
vmap defendedges_label2;
vmap defendedges_label3;
vmap bdefendedges_label2;
vmap bdefendedges_label3;
std::string eLine(int, int, int);
std::string vLine(int, int);
public:
Cgraph(){
};
~Cgraph(){ delete &vertices;
delete &utf; delete &wattackedges; delete &battackedges;
delete &defendedges_label2; delete &defendedges_label3;
delete &bdefendedges_label2; delete &bdefendedges_label3;};
在我遵循的主要方法中
Cgraph *graph = new Cgraph();
delete graph;
我收到此错误报告
*** Error in `./chess': double free or corruption (out): 0x00007ffffebb1340 ***
Aborted (core dumped)
您从未使用 new
显式动态分配这些成员,因此您不应该 delete
ing 它们。由于这些成员都有负责清理的析构函数,因此您不需要析构函数。
map 会在内部创建它所需的内存,当调用析构函数时,也会调用 map 的析构函数并清除所有数据。
我有一个问题,我不知道解决方法,需要你的帮助!提前致谢!
这里是我的class(class复制不完整)
#ifndef _CGRAPH_H_
#define _CGRAPH_H_
class Cgraph{
typedef std::list<int> vlist;
typedef std::map<int, int > vidmap;
typedef std::map<int, std::string > vutfmap;
typedef std::map<int, vlist > vmap;
typedef std::map<int, vlist >::iterator vmap_it;
int frequency;
private:
vidmap vertices;
vutfmap utf;
vmap wattackedges; // label 0
vmap battackedges; // label 1
vmap defendedges_label2;
vmap defendedges_label3;
vmap bdefendedges_label2;
vmap bdefendedges_label3;
std::string eLine(int, int, int);
std::string vLine(int, int);
public:
Cgraph(){
};
~Cgraph(){ delete &vertices;
delete &utf; delete &wattackedges; delete &battackedges;
delete &defendedges_label2; delete &defendedges_label3;
delete &bdefendedges_label2; delete &bdefendedges_label3;};
在我遵循的主要方法中
Cgraph *graph = new Cgraph();
delete graph;
我收到此错误报告
*** Error in `./chess': double free or corruption (out): 0x00007ffffebb1340 ***
Aborted (core dumped)
您从未使用 new
显式动态分配这些成员,因此您不应该 delete
ing 它们。由于这些成员都有负责清理的析构函数,因此您不需要析构函数。
map 会在内部创建它所需的内存,当调用析构函数时,也会调用 map 的析构函数并清除所有数据。