Proper cleanup?

Started by
9 comments, last by save yourself 20 years ago
If you use a std::vector which olds objects by value, you can just delete the vector (or allow it to fall out of scope normally) and the objects will be gone.

If as is the far more usual case, it is holding pointers, you need to delete the pointers too.

Typically you might have a GameManager class which holds a number of vectors of pointers to various things. The normal place to put this cleanup code would be in the GameManager destructor.

Alternatively, if you are using boost::shared_ptr instead of pointers, as long as you don''t have circular references, the objects will get deleted.

As other posters mentioned, the memory is not a big problem on program exit, as the OS will clean up any that gets left. But other objects like file handles, won''t get closed properly (for example if there is a buffered write pending, buffers might not get flushed and data lost) - this could cause file corruption (BUT unlike in DOS-like OSs, won''t cause *filesystem* corruption)

Also, leaving the graphics hardware in an unusual state can confuse things slightly. The OS tries to return things to their previous state, but in my experience, sometimes fails. So it''s good practice to call whatever cleanup functions are necessary. One things that I''ve seen before is Windows being left in a stupid video mode after a fullscreen app shuts down uncleanly.

Network things are less problematic, as a client-server system should be robust enough to sensibly handle an abrupt disconnect.

Mark

This topic is closed to new replies.

Advertisement