-3

I have a problem and I don't know the solution for it, need you help! Thank you in advance!

Here my class (class is copied not completely)

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

in the main methode I do following

Cgraph *graph = new Cgraph();
delete graph;

and I get this error report

*** Error in `./chess': double free or corruption (out): 0x00007ffffebb1340 ***
Aborted (core dumped)
malocho
  • 265
  • 3
  • 13
  • 5
    `delete &vertices` You haven't allocated them with `new` - you have no business deallocating them with `delete`. Drop all those `delete` statements - they are nonsense. You don't appear to need a user-defined destructor at all (nor default constructor, for that matter). – Igor Tandetnik Aug 02 '15 at 22:13
  • 7
    Literally delete your entire destructor. Problem solved. – Barry Aug 02 '15 at 22:14
  • 1
    A destructor should undo the actions of the constructor. Your constructor is empty, so your destructor should be empty, too. – melpomene Aug 02 '15 at 22:16
  • 1
    If C++ required to call `delete` on all the member variables, I would quit and use Java. – PaulMcKenzie Aug 02 '15 at 22:20

2 Answers2

3

You never explicitly dynamically allocated those members with new so you shouldn't be deleteing them. Since those members all have destructors that'll take care of cleanup, you don't need your destructor.

melpomene
  • 84,125
  • 8
  • 85
  • 148
Bizkit
  • 384
  • 1
  • 10
0

map will have internally created the memory required for it and when the destroctor is called the destructor of map also will be called and clears all data.

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

rams time
  • 179
  • 5