I have following piece of code:
customObject* object;
std::list<customObject> objects;
for(int i = 0; i < 10: ++i) {
object = new customObject;
object.getvalue(i);
objects.push_back(*object);
}
Will the memory be freed on successful exit?
Sorry, guys. Made some mistakes)) fixed
Once you fix the syntax errors and actually compile and run your code (assuming you keep in the
new), you will leak tencustomObjects.You must iterate over the
listanddeleteeach instance that you havenew‘d.Consider whether your
listneeds to contain pointers… If it does, then consider using a smart pointer (but not astd::auto_ptr).It is probably better just to store the objects themselves (which
std::list<customObject> objectsdoes).