Jump to content
  • Advertisement

Archived

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

save yourself

Proper cleanup?

This topic is 5285 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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 things like unreleased pointers, your RAM will slowly get more and more cluttered up until you run out.

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!