*myMap; to some class A. This...">

Valgrind gives "Invalid size 8 record" over empty destructor

I have declared a:

std::map<unsigned int, MyClass> *myMap; 

to some class A. This mapping is created in constructor A:

 myMap = new std::map<unsigned int, MyClass>; 

The MyClass class is basically a structure for storing some data using some getters / seters. There are no pointers or new instances in MyClass, just a pair of enum, unsigned int and bool values. Therefore, the MyClass destructor is empty.

On the other hand, in the destructor, I delete the card:

 A::~A(){ if(myMap!=NULL){ delete myMap; myMap = NULL; } } 

Here Valgrind tells me: β€œThe address 0x4c389b0 is 16 bytes inside a block of size 48 free'd [PID: 6077]” above the delete line.

Also, with the MyClass destructor, even when empty, I get an "Invalid size 8 entry [PID: 6077]"

I do not understand, there was a problem. I always thought that calling delete over a vector or map would automatically call each element corresponding to the destructor, in which case the destructor has nothing to do.

Any help?

EDIT: added constructor:

 A::A(unsigned int someValue){ m_someValue = someValue; initializeMap(); } void A::initializeMap(){ myMap = new std::map<unsigned int, MyClass>; for(unsigned int i=1; i<=20; i++) mymap->insert(std::make_pair(i, MyClass(i))); } 

In addition, the constructor of MyClass:

 SvAvailabitlity::SvAvailabitlity(unsigned int index){ m_index = index; //unsigned int m_Flag = false; //bool m_enumData1 = NOT_OK; //enum MyEnum m_enumData2 = NOT_OK; //enum MyEnum } 

Where MyEnum is defined as:

 typedef enum { OK = 0, NOT_OK = 1, } MyEnum; 

By the way, I do not understand the negative voice. Someone can also explain this, just in case, offend someone with this issue, which, I think, is right according to the rules of stackoverflow.

+4
source share
1 answer

The problem is a rule of three violations. Any copy of your object will result in a double release.

+2
source

All Articles