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)
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;
}
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 )
{
int nSize = m_Objects.size();
for(int i = 0; i < nSize; i++)
{
if(m_Objects[i].GetObjectID() == ulObjectID)
{
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.