C ++析构函数的问题

时间:2015-08-02 22:12:16

标签: c++ destructor

我遇到了问题,我不知道解决方案,需要你的帮助!提前谢谢!

这是我的班级(班级不完全复制)

#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)

2 个答案:

答案 0 :(得分:3)

您从未使用new明确动态分配这些成员,因此您不应delete这些成员。由于这些成员都有析构函数可以完成清理,所以你不需要你的析构函数。

答案 1 :(得分:0)

map将在内部创建它所需的内存,当调用destroctor时,也将调用map的析构函数并清除所有数据。

http://www.cplusplus.com/reference/map/map/~map/