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; // Here I get Heap Allocation error :(
}
m_data_vs_entity.clear();
}
bool CReader::process_entity(string section1,string section2)
{
size_t length;
char buffer[9];
length = section1.copy(buffer,8,0);
buffer[length]='\0';
int ent_type = atoi(buffer);
CEntity* current_ent = NULL;
switch(ent_type)
{
case 110:
{
CLine* line_entity = new CLine(section1,section2);
current_ent = line_entity;
break;
}
default:
{
break;
}
if(current_ent != NULL)
{
// One of the get methods from CEntity class get's called ...
int PD_Pointer = current_ent->get_parameter_data_ptr();
m_data_vs_entity.insert(pair<int,CEntity*>(PD_Pointer,current_ent));
}
returntrue;
}
I am reading the data from a file and then populating the CLine Class.The map gets populated in a function process_entity 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.Can anybody find what's wrong in here please ? In the meantime, I tried using application verifier to track memory related issues, but it doesn't seem to find anything. Also ,i read somewhere in this forum about HEAP corruption causing due to Pointers not initializing properly. I guess I am doing that too. Any help ???
I am using VC++ 2008.
Thanks in advance and awaiting reply,
Regards,
Atul
I can't see anything wrong in what you've posted. But I have a few comments.
1. Is this really minimal code that reproduces the problem?
2. You don't need to clear the map in CReader's constructor. The map initialises itself.
3. CEntity has a create method, but you still do new CLine. Why?
4. Your braces in CReader::process_entity don't match up. You'll have a syntax error.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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. Any guidelines on Heap allocation error: after normal block (XXX) at XXX, CRT detected that application wrote to memory after end of Heap buffer.
will help me.
Thanks,
Atul
The beauty about heap corruption detection errors is usually: It's somewhere completely else!
The place where the error gets detected is most probably not the place where you have that nasty out-of-bound access to some array or the wrong for-loop-condition etc..
So as soon as you get heap corruption, think of "What did i do last?" and not the usual "What's with this place, the compiler showed me?"
The code above with the buffer copying looks fine to me, although you can do the same with string.substr, which may or may not be faster. ;). Beside - you are not using the heap there. buffer is on the stack and you are not writing to any other pointer..
Finally, after two days of debugging, I was able to fix up the crash. It was due to me copying wrong number of characters from the string.
Lessons learnt :
1. When you encounter memory allocation errors, try to form a simple test case which has minimum entities to reproduce the problem.
2. A sure shot way is a line by line debugging. I agree,it test your patience, but then, there are no short cuts to success
3. And it gives you a chance to do a code review, further enhancing the quality of code that you produce in future
Thank you for all your help and formatting my code :)
Regards,
Atul