Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is the risk when using TerminateProcess?

My Win32 console applicaton uses a third-party library. After it exits WinMain global objects destruction begins and an AV happens somewhere deep inside. I'm really tempted to just write

TerminateProcess( GetCurrentProcess(), 0 );

somewhere near the end of WinMain. If I do this the application ends gracefully.

But MSDN says that doing so can compromise the state of global data maintained by dynamic-link libraries (DLLs) which is not clear. I understand that if I have some global object its destructor is not run and I risk not finalizing a database connection or something similar. I don't have anything like that in my program.

What exactly is the risk when using TerminateProcess? How do I determine if I can use it for my purpose?

like image 863
sharptooth Avatar asked Aug 14 '09 05:08

sharptooth


2 Answers

Based on the documentation for that and ExtiProcess it seems the primary concern is that DLL's are unloaded without a call to DllMain with the flag DLL_PROCESS_DETACH.

My 2cents: The documentation is being paranoid that you will upset some critical operation which runs in DllMain + DLL_PROCESS_DETACH. Anyone who depends on that to maintain critical state is already at the mercy of task manager so I don't see a huge risk in using this API.

like image 171
JaredPar Avatar answered Nov 09 '22 22:11

JaredPar


Generally the bad things will happen when interacting with objects outside of your process. For an example say you have some shared memory used by multiple processes that your process will write to and other processes read and or write to. Typically to synchronize the reading and writing a mutex is used. If a thread in your process has acquired the mutex and is in the middle of making changes when TerminatePorcess is called, the mutex will be abandoned and the shared memory potentially left in an inconsistent state.

I suspect you are miss using one of the third party libraries. DllMain is somewhat limiting so the library may have initialize and uninitialize functions that you are supposed to call.

like image 24
Stephen Nutt Avatar answered Nov 10 '22 00:11

Stephen Nutt