0

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;
    }
}
user3294816
  • 37
  • 1
  • 7
  • Strongly Recommend against using `malloc`/`free` – user4581301 Apr 23 '17 at 03:30
  • Are you familiar with [The Rule of Three?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – user4581301 Apr 23 '17 at 03:31
  • The destructor will not be called twice for one object, unless you explicitly make that happen. What is probably happening is that you have two objects containing the same value of `mBuffer`, and therefore are releasing it twice. Look up "rule of three" for a technique to address that (or its cousin "rule of five" in C++11). Also, use operators `new` and `delete` in C++ to allocate/deallocate objects - `malloc()` and `free()` don't work as well. – Peter Apr 23 '17 at 03:31
  • We need to see the `MyObject` copy constructor. – Potatoswatter Apr 23 '17 at 03:32
  • @Potatoswatter: odds are the OP hasn't implemented one. That's why user4581301 and I have advised looking up rule of three. – Peter Apr 23 '17 at 03:33
  • @Peter `malloc` and `free` work with POD types, or to obtain raw byte buffers. There are more C++-ish alternatives, but depending on what happens to the memory buffer, it should be fine. – Potatoswatter Apr 23 '17 at 03:35
  • @Peter Also why I asked… but I think that's better than tacitly assuming. – Potatoswatter Apr 23 '17 at 03:35
  • I don't have a copy constructor implemented – user3294816 Apr 23 '17 at 03:36
  • @Potatoswater - I'd estimate about 70% of the time I see questions like this that the OP either hasn't even considered the need to implement a copy constructor, so being pointed to rule-of-three is appropriate. If those odds change, I'll stop tacitly assuming that is the case. – Peter Apr 23 '17 at 03:42
  • I'm still not sure why the destructor is invoked twice at exiting this function. For example, if the if block is executed, curobject loses scope and is deleted. What is the second object getting destroyed? – user3294816 Apr 23 '17 at 20:50

0 Answers0