I am trying to add a custom object to my map provided it doesn't already exist. If it exists, I am calling a function that adds to my existing buffer for the custom object. When this function is executed, I see the destructor being called twice on the CustomObject and as a result, the second call to destructor causes an "Invalid address passed to free" problem. I am new to map and would appreciate some tips on fixing the code. I am assuming that calling insert on the map would make a copy of myobject and that would remain in scope as long as the map stays in scope.
MyObject::MyObject():
mBuffer(nullptr)
{
}
MyObject:: ~MyObject()
{
free(mBuffer);
mBuffer = nullptr;
}
MyObject::Append(p_someData):
{
//Calls malloc on mbuffer and copies data from someData into mBuffer
}
void AnotherObject::AddResulttoMap(const p_otherobject *result,uint32_t index)
{
MyObject curObject;
if (mMyObjectsMap.find(index) == mMyObjectsMap.end())
{
curObject.Append(result->result);
mMyObjectsMap.insert(std::pair<uint32_t, MyObject >(index, curObject));
std::cout << "DEBUG: Writing new object for index" << unsigned(index) << std::endl;
}
else
{
curObject= mMyObjects.find(index)->second;
curObject.Append(result->result);
std::cout << "DEBUG: Appending into result for index" << unsigned(index) << std::endl;
}
}