Archived

This topic is now archived and is closed to further replies.

save yourself

Proper cleanup?

Recommended Posts

What sorts of things should I make sure that I am cleaning up before my program finishes running? The only thing I have really been worrying about so far is making sure my sockets have been closed (shutdown socket, call recv, close) and calling WSACleanup(), so basically I''m just wondering what else I should be doing (structures, vectors, global variables, etc)? Any insight would be appreciated and thanks in advance.

Share this post


Link to post
Share on other sites
Anything you do that affects the system, you should undo:

- if you open files, close them
- if you allocate mem, free it
- if you change the screen resolution, restore the old resolution
- if you create temp files, delete them
- etc.

Share this post


Link to post
Share on other sites
The only dynamic memory that I allocate is done for me when using vectors (as far as I know). Would resizing all vectors to 0 be the correct way to free this memory? Does memory allocated at startup (not dynamically) have to be freed by me as well?

Also, what happens if for some reason my code crashes and winsock isn''t shut down properly and/or memory isn''t freed properly. Is this a very bad thing? How can it be fixed if it happens (I am already error checking, but you never know...)

Share this post


Link to post
Share on other sites
with vectors call clear() on the vector object which deletes all elements stored in the vector.

[edited by - Spudder on April 3, 2004 4:30:41 PM]

Share this post


Link to post
Share on other sites
quote:
With things like unreleased pointers, your RAM will slowly get more and more cluttered up until you run out.


When your program stops, windows releases all the ram it used anyhow. Memory leaks are only an issue while the program is running. It''s resource leaks that are trouble - things like window handles, for instance.

If your program crashes and winsock doesn''t get closed, I don''t see how it can do anything worse then make whatever ports you were using unavailable until you restarted. I know I''ve crashed a few winsock programs, and that''s the worst problem I encountered.

Share this post


Link to post
Share on other sites
quote:
Original post by save yourself
The only dynamic memory that I allocate is done for me when using vectors (as far as I know). Would resizing all vectors to 0 be the correct way to free this memory? Does memory allocated at startup (not dynamically) have to be freed by me as well?



You shouldn''t have to do anything to the vectors, nor non-dynamic mem. Basically, if you ever use "new" or "malloc(...)", then you need to use "delete" or "free(...)".

Are you talking about STL vectors? They''ll clean up themselves.

quote:

Also, what happens if for some reason my code crashes and winsock isn''t shut down properly and/or memory isn''t freed properly. Is this a very bad thing?



It''s not good, but don''t worry too much about it, unless you want to write some robust exception handling. I think sockets will close themselves after a while (can someone confirm?)

Share this post


Link to post
Share on other sites
You don''t need to clear vectors. The point of vectors is that they manage their own memory.

Share this post


Link to post
Share on other sites
Actually that depends: If you use "normal" std::vector then they clean themselves up, however if your vector contains pointers to data instead of the data itself then that vector does NOT clean itself up. You need to go through each cell and delete whatever your pointer is pointing to manually. Calling clear on that vector wouldn''t work either because it''d delete the pointers themselves, and then you wouldn''t have anything pointing to your data - but your data would still be sitting there nice and tight - which is basically the definition of a gigantic memory leak =D

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites