Click here to Skip to main content
14,922,577 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello once again,

I was hoping you might want to explain me why my application crashes on std::vector erase.
So let me introduce you into my code:

I have packet stream. I defined the packet where the informations about object are set.
Then there is class for this object:

Description of CObject:
CObject -
It has 3 member variables which points to the heap. Two of those variables are optional.
(I will use some random names for those variables, it doesn't really matter)
m_chObjectName
m_chObjectDescriptor
m_chObjectSomething

Facts:
The CObject has constructor which sets these pointers to zero.

The CObject has copy constructor which:
a) Sets the pointers of destination object to zero
b) Checks the pointers of source object if they are not zero (Because of optionality)
if( ObjectSource.m_chObjectName != 0)
  //copy objectSource.m_chObjectName into newly allocated memory for this->m_chObjectName;

c) Copies rest of the variables.
this->m_nID = ObjectSource.m_nID
etc.

The CObject has it's member method which takes buffer of packet as parameter and sets all corresponding variables of the class to the values specified in the buffer. The signature of method is void CObject::SetObjectByPacket(unsigned char * byObjectPacket)

The object destructor:
a) Checks if pointer == 0 and if not delete it
	if(m_chObjectName)
	{
		delete [] m_chObjectName;
		m_chObjectName = 0;
	}
//etc.


Description of CObjectWrapper
This class is like interface class for CObject

It has one std::vector <cobject> member variable called m_Objects.
It has 2 member methods:
a)
void CObjectWrapper::InsertObjectFromPacket(unsigned char * byDataBuffer)
{
	CObject newObject;

	newObject.SetObjectByPacket(byDataBuffer);

	m_Objects.push_back(newObject);
}

b)
void CObjectWrapper::DeleteObjectByID( unsigned long ulObjectID ) // ID is obtained from packet
{
	int nSize = m_Objects.size();

	for(int i = 0; i < nSize; i++)
	{
		if(m_Objects[i].GetObjectID() == ulObjectID) // GetObjectID return ID of CObject class
		{
			m_Objects.erase(m_Objects.begin()+(i));
			
			break;
		}
	}
}





So where is the problem:
The application crashes after some time on
m_Objects.erase(m_Objects.begin()+(i));
while calling destructor of that CObject. The heap pointers of that CObject are something strange but certainly not zero. I could bypass that by using static size for those names and descriptors, but this bugs me so I want to know the answer.

Thank you for you help.
Posted
Comments
Cedric Moonen 20-Jun-11 2:22am
   
Could you also post the exact error message displayed when your code crashes ?
Im2N00By 20-Jun-11 10:31am
   
Problem solved, but thank you for interest.

1 solution

Implement an override for the assignment operator, which is not the same as a copy constructor. The default operator does a member-wise copy, which in most cases isn't desired if you have pointers.

That is probably the cause of your problem, since the copy constructor seems ok.
   
Comments
Im2N00By 20-Jun-11 10:30am
   
Ahh, I should finish the book first and then I should play around. Thank you a lot, you are The King. I guess the destructor will take care of the allocated memory, or is there something else I should add ?(I don't want to risk memory leak time bomb)
Niklas L 21-Jun-11 2:45am
   
As a rule of thumb, if you implement the copy constructor, you should implement the assignment operator as well.

Member pointer variables you have allocated memory for, need to be deleted in the destructor. This seems to apply to your implementation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900