Thread: Closing a program - what happens

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    69

    Closing a program - what happens

    I have written a small console application with this structure:

    Code:
    int main()
    {
       AllocateSomeMemory();
    
       while(1)
       {
          Sleep(30);
          DoSomeThings();
       }
       DeallocateSomeMemory();
    }
    If I close the application, will the memory be deallocated? More precisely: will my code that deals with deallocating the memory be executed, or will the OS (WindowsXP) handle this for me? What is the 'correct' way to deal with a this (a loop that should run untill the application is closed)?

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    If I remember rightly all the memory is freed up after the program closes, it is just if you reallocate before deallocating then you cause a memory leak. I think I right though, I am not 100% sure.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    You are correct bumfluff. The thing to watch for is pointer allocations on program close.
    If for example you allocate a pointer variable space on the heap using "new" then you MUST call "delete" before the return 0 statement to ensure the memory is freed once more. You can if then wish assign that pointer to NULL to deter any possible memory leak, but that is a preference not an absolute.
    As a side note, any allocations I do with new are deleted and NULLED out of habit.
    Double Helix STL

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://c-faq.com/malloc/freeb4exit.html
    Having said that, it's good practice to try and make your program leak-free from the outset.

    > What is the 'correct' way to deal with a this (a loop that should run untill the application is closed)?
    Write it as a windows application (with a winmain() and a message loop), and respond to the WM_CLOSE event in the appropriate manner.

    Or have something like
    while ( !done )
    rather than
    while ( 1 )
    and a mechanism somewhere in your code to set that flag at the end of the program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    69
    Thanks.

    So it's only possible to let my code react to the close event in a windows application. I'll have to learn about that then.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    MS-Windows specific: console program can call _onexit() to install an event handler which is supposed to get called whenever the program terminates for whatever reason.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Sorry to interject, but:

    Is there any mechanism to release memory when a program crashes, or is it lost until the os reboots?

    Do Windows, Linux, OS X differ in this regard?

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    13
    Quote Originally Posted by Ancient Dragon
    MS-Windows specific: console program can call _onexit() to install an event handler which is supposed to get called whenever the program terminates for whatever reason.
    isn't there a standard atexit function?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Do Windows, Linux, OS X differ in this regard?
    Like my link said, all good operating systems free all the memory associated with a process when that process dies - for whatever reason.

    The OS will simply deallocate all the virtual memory associated with the process.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Thanks, Salem. I didn't notice the link.

    Guess it was too red...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. exiting and closing a program
    By major_small in forum C++ Programming
    Replies: 10
    Last Post: 05-30-2003, 08:31 PM
  3. How to restart a program without closing it...
    By D4050 in forum C++ Programming
    Replies: 16
    Last Post: 10-31-2001, 12:38 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM