PDA

View Full Version : C++ resources release


pepribal
24th February 2007, 09:14 AM
Let's say I have several objects, and each has several resources: file descriptors open, opened sockets, allocated memory, and so on... What happens if at some point of the code I must exit with exit()? What happens with all those resources? Are they freed by some sort of garbage collector? Does it depend on the platform?

bman
24th February 2007, 10:44 AM
As far as I know ( and I could be wrong on this! ) once the program finishes, all memory is freed by the OS as the process is no longer running. Write a program which allocates a lot of memory and doesn't free it. Type top into another terminal and watch the RAM get used up. Then quit the program and have a look at top again. All the memory been used by the process should be freed.

baschti
24th February 2007, 01:45 PM

hi pepribal,
if you end your programm with exit the destructors of those objects will be called automatic - so this is the
right place to tidy up, e.g. close file descriptors
but even if you have no destructor defined the used memory will be marked as free

mwette
24th February 2007, 02:55 PM
The OS will clean up memory and open files, and reparents any child processes to the "init" process.
You don't have to worry that everything is cleaned up. But it is still good practice to free() memroy
close() files, and wait() on child processes.

pepribal
24th February 2007, 06:05 PM
However, I added some code that sends a string to the standard output (inside the destructors code), and after the exit() call nothing appeared in the console. Is this because the destructors are called after the OS has reparented the exiting process to init (so that the parent is not that shell/terminal anymore, so standard output doesn't appear there) ?

mwette
24th February 2007, 06:12 PM
However, I added some code that sends a string to the standard output (inside the destructors code), and after the exit() call nothing appeared in the console. Is this because the destructors are called after the OS has reparented the exiting process to init (so that the parent is not that shell/terminal anymore, so standard output doesn't appear there) ?

You should flush.
If you were using printf in C then you would call "fflush(stdout)".
In C it might be "cout.flush()"

pepribal
24th February 2007, 06:49 PM
I tried, flushing the output in the destructor, but the results are the same: the destructors are only called if I comment out the "exit(1)" just before the "return EXIT_SUCCESS" sentence in my main(). Maybe it's a normal behaviour?

mwette
24th February 2007, 07:04 PM
I think what you want to do instead of calling exit(1) is to "return EXIT_FAILURE" (from main())

pepribal
24th February 2007, 07:28 PM
Well, actually that is not a problem, as I placed the exit(1) there as means to make the experiment... I was just curious why the destructors output is not seen in the console... Any idea?

mwette
24th February 2007, 07:42 PM
You are not giving a lot of details, but exit() does not return, it exits the process.
If you want the global destructors called you need to return from main().
Try "man -s3 exit". There are other methods for getting things done (e.g., "man atexit").

pepribal
24th February 2007, 08:48 PM
So that's what I wanted to know: no return from main(), no destructors are called. Thanks!

(Sorry for not posting any code, but it was a general question)