Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I am getting a Heap Corruption Error?

I am new to C++. I am getting HEAP CORRUPTION ERROR. Any help will be highly appreciated. Below is my code

class CEntity  
{  
//some member variables  
CEntity(string section1,string section2);  
CEntity();  
virtual ~CEntity();  
//pure virtual function ..  
virtual CEntity* create()const = 0;  
};  

I derive CLine from CEntity as below

class CLine:public CEntity  
{  
// Again some variables ...  
// Constructor and destructor  
CLine(string section1,string section2);  
CLine();  
~CLine();  
CLine* Create() const;  
}  

// CLine Implementation  
CLine::CLine(string section1,string section2) : CEntity(section1,section2){};  
CLine::CLine();  
CLine* CLine::create() const {return new CLine();}  

I have another class CReader which uses CLine object and populates it in a multimap as below

class CReader  
{  
public:  
CReader();  
~CReader();  
multimap<int,CEntity*>m_data_vs_entity;  
};  

//CReader Implementation  
CReader::CReader()  
{  
  m_data_vs_entity.clear();  
};  

CReader::~CReader()  
{  
  multimap<int,CEntity*>::iterator iter;  
  for(iter = m_data_vs_entity.begin();iter!=m_data_vs_entity.end();iter++)  
  {  
    CEntity* current_entity = iter->second;  
    if(current_entity)  
      delete current_entity;  
  }  
  m_data_vs_entity.clear();  
}  

I am reading the data from a file and then populating the CLine Class.The map gets populated in a function of CReader class. Since CEntity has a virtual destructor, I hope the piece of code in CReader's destructor should work. In fact, it does work for small files but I get HEAP CORRUPTION ERROR while working with bigger files. If there is something fundamentally wrong, then, please help me find it, as I have been scratching my head for quit some time now.
Thanks in advance and awaiting reply,
Regards,
Atul

Continued from Y'day : Further studying this in detail I now have realized that Heap allocation error in my case is because I am allocating something, and then overwriting it with a higher size. Below is the code where in my data gets populated in the constructor.

CEntity::CEntity(string section1,string section2)
{
    size_t length;
    char buffer[9];
    //Entity Type Number
    length = section1.copy(buffer,8,0);
    buffer[length]='\0';
    m_entity_type = atoi(buffer);

    //Parameter Data Count
    length = section1.copy(buffer,8,8);
    buffer[length]='\0';
    m_param_data_pointer = atoi(buffer);
        //.... like wise ....
}

I am getting the values at a fixed interval of 8 chars and I am adding a '\0' so this, i guess will take care of any garbage value that I encounter.

About Heap allocation error: after normal block (XXX) at XXX, CRT detected that application wrote to memory after end of Heap buffer. Mostly, Heap allocation errors occur somewhere else than where it crashes. I would appreciate if some one here would help me, how to make use of this normal block and the address. Thanks,

like image 770
Atul Avatar asked Feb 22 '26 22:02

Atul


1 Answers

Well, you're only showing half the problem.

Where's the code that creates the CLine objects and stores them in the CReader?

Also, what do you consider actually "owns" the CEntity objects? Generally, you should make the 'owner' responsible for creation as well as deletion...

I wrote earlier:

"You might like to consider storing the CEntitys directly in the map, rather than storing pointers. Potentially less efficient, but also much less scope for cockups."

As Neil points out, it's not CEntities that you will be storing, so that suggestion isn't going to help you much...

like image 86
Roddy Avatar answered Feb 25 '26 15:02

Roddy