the object itself?
I have a problem.. when the list is destroyed...
as an example of what my code does....
What is the problem?
std::list<myObject> myListOfObjects;
myObject obj;
for i=0; i < n; ++i
{
obj.modifySomeProperty;
myList.push_back(obj);
}
When the function returns the list is destroyed by going out of
scope.. and of course so is obj.
What function?
First off ... Maybe I should have created a new instance of myObject
on each iteration of the loop or perhaps using the same one over and
over again is OK.... (as long as push_back is making a copy of the
object.)
Suggestions?
Yes. Ask what you want to know?
void MyFunc()
{
std::list<MyObject> myList;
MyObject myObject;
for(int i=0; i < 10; ++i) {
myObject.modify();
myList.push_back(myObject); // Makes a copy
}
// do some stuff with my list
} // dtor of myObject, myList (an all objects in myList)
If the question is how many MyObjects exist at the end of the
function,
the answer would be 11. The one on the stack and the 10 in the list.
And yes all 11 dtors would be called.
If the question is, are there memory leaks? Insufficient info. Need to
know much more about MyObject. What does it contain? Does it need a
copy constructor? Is one defined?
The class defines operator=, a copy constructor and a destructor.
but the program crashes during the destruction of the second object..