I have a std::list of objects.
When I create a local variable object and push it onto the list, I am getting a debug assertion error when the local variable gets destructed (at the return of the function or with a delete call in the case of a dynamically allocated one).
The copy constructor is rather simple: Menu( const Menu& am ):
mpInput( am.mpInput ),
mpCurbox( am.mpCurbox ),
mpCursor( am.mpCursor ),
mItems( am.mItems ),
mBinds( am.mBinds ){}
mpInput, mpCurbox, and mpCursor are all pointers. mItems and mBinds are lists.
Here is where the error occurs (actx is passed into this function by reference): Gui::Menu lmenu = Gui::Menu( actx.mpInput, &actx.mpGui->mBoxes.front(), litems, lmenuadds );
actx.mpGui->mMenus.push_front( lmenu );
return lreturn;<---As soon as the function returns, a 'Debug error!' is comes up and when I break the function is in xutility line 147 __CLR_OR_THIS_CALL ~_Iterator_base()
{ // destroy the iterator
_Lockit _Lock(_LOCK_DEBUG);
_Orphan_me();
}
It was my understanding that std::list creates a copy of the object, and would do so with the copy constructor? If so I do not know why this error would occur when the local object is destructed.
Your Menu class is probably deleting / freeing memory for these objects in the destructor, right?
So what's happening here is that you're copying a pointer, then deleting the data, while still trying to use that pointer elsewhere.
It's similar to this:
1 2 3 4 5
int* a = newint(5);
int* b = a; // copy the pointer
delete a; // delete the original pointer
*b = 5; // now this explodes because the int has been deleted!
What you might need to do is do a deep copy of the pointers. It might be as simple as this:
1 2 3 4 5 6
Menu(const Menu& am) :
mpInput( new Input( *am.mpInput) ),
mpCurbox( new Curbox( *am.mpCurbox) ),
mpCursor( new Cursor( *am.mpCursor) ),
mItems( am.mItems ),
...
Note that you'll have the exact same problem with your assignment operator. So your assignment operator will need similar reworking.