views:

236

answers:

6

I currently have a program which has the following basic structure

main function -- displays menu options to user -- validates user input by passing it to a second function (input_validator) -- if user selects option 1, run function 1, etc

function1,2,3,etc -- input is requested from user and then validated by input_validator -- if input_validator returns true, we know input is good

Here is my problem. I want to allow the user to quit at any point within the program by typing '0'. I planned on doing this with some basic code in input_validator (if input = 0, etc).

This would appear to be simple, but I have been told that using quit() will result in some resources never been released / etc. I cannot simply do a 'break' either -- it will result in my program simply returning to the main function.

Any ideas?

+4  A: 

exit()

Terminates the process normally, performing the regular cleanup for terminating processes.

First, all functions registered by calls to atexit are executed in the reverse order of their registration. Then, all streams are closed and the temporary files deleted, and finally the control is returned to the host environment.

Amber
This is telling you what is actually released back, but not what may be left: named pipes, non-temporary files that are supposed to be deleted before exiting (i.e. some processes store the pid in a file in linux, that file is not temporary, but should be deleted on program exit)... Probably not a problem in the OP program, but still.
David Rodríguez - dribeas
+1  A: 

exit(int exitCode) - defined in stdlib.h / cstdlib - you'd probably want to exit(0); // normal termintation.

James
+4  A: 

One possibility would be to do it by throwing an exception that you catch in main, and when you catch it, you exit the program. The good point of throwing an exception is that it lets destructors run to clean up objects that have been created, which won't happen if you exit directly from elsewhere (e.g., by using exit()).

Jerry Coffin
Its been awhile since I programmed in C++, any good tutorials you know of on exceptions?
BSchlinker
@BSchlinker:Probably the best tutorial on exceptions (though likely more detailed than you need immediately) is *Exceptional C++*, by Herb Sutter. That's an expansion of material from his old Guru of the Week Usenet posts, available at http://www.gotw.ca.
Jerry Coffin
+3  A: 

This hasn't been true for any kind of mainstream operating system for a long time. The OS ensures that all kernel resources are released, even if the program didn't explicitly do so. Calling abort() or exit() from anywhere in your code is fine.

Hans Passant
Some mobile OS still have problems dealing with this, and there are resources that the OS cannot know that they need to be released. If the program created named pipes, or files whose duration is supposed to be only the program lifetime (i.e. pid files in some linux apps), backup files (i.e. in some editors and word processors)...
David Rodríguez - dribeas
A: 

exit() will not call your destructors, so you might want to consider using an exception handler instead.

If you have things like open but unflushed files, the OS will close the file handles, but won't flush any unwritten data.

MarkR
A: 

You have to design your menu system so that a status can be passed back to the previous method, unwinding until code in the main function is executed. Similar issues apply to back or previous screen buttons.

Taking a step back and looking at the Big Picture, the unwinding technique looks very similar to C++ exception handling strategy. I suggest using exceptions for cases that don't follow the normal flow of execution, such as main menu, and previous menu.

Try it out.

Thomas Matthews