What is the risk when using TerminateProcess?

My Win32 console applaton uses a third-party library. After it exits WinMain, global objects are destroyed, and AV happens somewhere deep inside. I really want to just write

TerminateProcess( GetCurrentProcess(), 0 );

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

But MSDN says this could compromise the state of global data supported by dynamic link libraries (DLLs) that are not clear. I understand that if I have a global object, its destructor does not start, and I risk not modifying the database connection or something like that. There is nothing like this in my program.

What exactly is the risk of using TerminateProcess? How to determine if I can use it for my purposes?

+5
source share
4 answers

As a rule, bad things happen when interacting with objects outside your process. For example, you have shared memory used by several processes that your process will write to, and other processes to read and write. Typically, a mutex is used to synchronize reading and writing. If your process thread has acquired a mutex and is in the middle of making changes when calling TerminatePorcess, the mutex will be deleted and the shared memory will potentially remain in an inconsistent state.

, . DllMain , , .

+5

ExtiProcess , , DLL DllMain DLL_PROCESS_DETACH.

2: , , DllMain + DLL_PROCESS_DETACH. , , , , API.

+6

AFAIK, "" (, , , , COM-), . Earwicker , , OS-wide, DLL, , , , , .

, ? , . , , , , . , , main() , .

+6

, " ". , ( ) , , - , , ​​ , ?

, OS , DLL, . , , , ; , , , , .

+4

All Articles