Thread: Why is it important to delete objects?

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Why is it important to delete objects?

    I was coding the part of my game were it deletes all the bitmaps when the game ends, and I thought, "Why am I supposed to do this". So What's the reason?

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    In order to free up the memory allocated by those objects.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    When the game ends? Usually nothing: usually the operating system will clean it for you. But it is bad habbit to start relying on it, especially if you move to a system that doesn't do that in the future. Also, future edits in the code might become fairly confusing: imagine someone decides to add something else at the end, not realising so much memory is still in use.

    Just destroy *anything* when you no longer need it (unless you cache it for later).

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    Ok thanks.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    68
    Well, just to make sure you understand.


    In the c/c++ programming languages, if you create (allocate) an object with the new operator, and you do not destroy (de allocate) the object with the delete operator, then you will have whats called a memory leak.


    When you use the new operator, your program is asking the OS for a specific amount of memory, when you call the delete operator, you are telling the OS that you are finished with the memory. If you never tell the OS you are done with the memory, then the OS assumes the memory is still is use --thus creating a memory leak.

    So, what ends up happening is that after you close your program, you may notice that you don't have as much available ram as you would think because the OS is reporting that it is still in use by a program.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    On Windows, when the current process terminates, all the memory allocated for that process is being freed. But just as many other said, by not explicitly freeing memory you are probably creating a memory leak bug when your code might be used in another way.
    Devoted my life to programming...

  7. #7
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    It's worth noting that the newer languages don't require you to explicitly free/delete things, instead their runtime has a thread that sits in the background, makes a note of your allocations and periodically frees it in one go (garbage collection).

    Just a different paradigm, but bear in mind that the thread might decide to do this when your code needs more memory and will hold your code up.

    I prefer the manual approach.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I prefer the manual approach.
    ....as do I....and I suspect the majority of the members here would share that sentiment.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    68
    On the Windows platform, in c/c++ if you allocate memory with the new, and DO NOT release it with a delete, the memory WILL NOT BE FREED with the program terminates.

    Please, read up, http://en.wikipedia.org/wiki/Memory_leak
    Last edited by smasherprog; 10-02-2010 at 12:23 PM.

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    It will be freed when the application exits. Runtime leaks will not be fixed if the program does not exit so if a runtime leak is left unchecked and your app is allowed to run then your program will eventually crash.
    Last edited by VirtualAce; 10-02-2010 at 03:54 PM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by SMurf View Post
    It's worth noting that the newer languages don't require you to explicitly free/delete things, instead their runtime has a thread that sits in the background, makes a note of your allocations and periodically frees it in one go (garbage collection).

    Just a different paradigm, but bear in mind that the thread might decide to do this when your code needs more memory and will hold your code up.

    I prefer the manual approach.
    C++ also has RAII to free memory, eg: smart pointers.
    Set them, forget them, so to speak. Only be sure to choose the proper smart pointer.

    This is the preferred way in modern C++, though I don't know if this is the method you "prefer." Though I certainly prefer it. What I do hate is garbage collection, though. It has no business in apps.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Personal opinions aside, with a question like "why is it important to delete objects?" the principle answer is because C++ does not have garbage collection built in. So avoiding mention of garbage collection would mean avoiding the answer IMHO.

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    modern C++,
    I still fail to understand your modern and non-modern approach to C++. Most of us are not that exclusive when it comes to modern or non-modern. The pure and simple point of this particular thread and of C++ is....clean up your own garbage. Whether you do that through your own means or some other means as long as the job gets done that is all we care about.
    The how is more about implementation specific choices than about fundamental memory management concepts. Smart pointers, while nice, are not the only way or the correct way in every case to do memory management. They are one very nice way but what they provide can be done manually if one would choose to go that route. Again it depends on the requirements of the application.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    When I say modern C++, I naturally refer to idoms and techniques typically used today. If you're writing a GUI application, there is a 99% chance you're using a smart pointer if you're using dynamic memory.
    Of course you're right in that smart pointers are not always the right tool. That is why we have a choice, after all.
    But technically, I wouldn't call it a burden anymore. we set a smart pointer and we forget it. The only important thing is the design. We cannot escape that we must plan the use of a proper smart pointer.
    Also note preferred. There are many things that are preferred, and sometimes you just have to go against those principles in order to meet the goal of the project. And that is fine, so long as it is justified.

    In the end, you are free to what you want, but I just wanted to point out that that there are practices and idoms in C++ today, and one of them is to use a smart pointer, when applicable.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    If you're writing a GUI application, there is a 99% chance you're using a smart pointer if you're using dynamic memory.
    This is too broad of a statement to be true.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-15-2010, 07:53 PM
  2. Objects as attributes of other objects.
    By kbro3 in forum C++ Programming
    Replies: 10
    Last Post: 08-15-2009, 03:46 PM
  3. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  5. how to delete objects
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 08-31-2001, 02:06 AM