Why I am getting a Heap Corruption Error?

Posted by vaidya.atul on Stack Overflow See other posts from Stack Overflow or by vaidya.atul
Published on 2010-03-15T11:54:03Z Indexed on 2010/03/15 11:59 UTC
Read the original article Hit count: 520

Filed under:

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

© Stack Overflow or respective owner

Related posts about c++