Reputation: 1212
I have a problem that I can't seem to find the answer to, but I'm willing to bet it's probably a pretty simple one for anybody who has worked with this sort of application setup before.
I'm working with an application that is written in C, but calls a TCL/TK gui to do lots of nice pretty stuff on the screen. However, due to the way that TCL/TK works when run from C, once you've handed over control- you never get it back (i.e. the TCL/TK interpreter handles the application exit, rather than returning to the original main() function).
So basically, there is some code like this:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow){
Tk_Main(1,argv,Tcl_AppInit); // Tcl_AppInit is a function that runs at the start
// All code after this point will never, ever run
return 0; // This return never actually occurs
}
This causes a slight issue. I want to be able to destroy data structures, close files, check for memory leaks, and do other necessary things before the program exits. However, I can't do it after Tk_Main. I have to figure out the hook in the TCL/TK interpreter that runs when it shuts down, then tell that to run my teardown function before quitting.
Anybody know how or where to do this?
Upvotes: 0
Views: 964
Reputation: 7237
There are in fact a few ways to do it:
Preferred way to handle your case would probably be 2. The manpage for Tcl_Exit() has all the details you should need: http://www.tcl.tk/man/tcl8.5/TclLib/Exit.htm
Upvotes: 2
Reputation: 434615
You could try using atexit()
. You pass it a void (*f)(void)
function to be called when the process terminates. I'm not sure how well that works under Windows though.
A more Tcl/Tk-ish approach would be to use Tcl_CreateExitHandler
to register an exit handler:
Tcl_CreateExitHandler arranges for proc to be invoked by Tcl_Exit before it terminates the application. This provides a hook for cleanup operations such as flushing buffers and freeing global memory.
Upvotes: 1