0

I have an MFC C++ program that occasionally crashes on an error exit for no apparent reason. My specific query is in the following:

In my overload of InitInstance() there are various checks, as well as setting up resources like events and semaphores. When a normal exit is requested by the user, there is an OnDestroy() message handler that closes everything out, including freeing resources, etc. That bit works fine.

The problem occurs when one of the checks in InitInstance() fails, and the function returns value 0 (causing a program exit). My question is this: In that event, is OnDestroy() automatically called to clean things up, or do I need to run it myself before exiting InitInstance?

BruceV
  • 117
  • 2
  • 10
  • Why don't you ask your debugger instead? Set a breakpoint in `OnDestroy()` and see what happens. Also, a process doesn't crash *"for no apparent reason"*. The reason becomes apparent, when you set up your debugger to break, when a Win32 exception is raised. Don't be helpless. – IInspectable Feb 18 '17 at 10:23
  • Any thread or mutex in your program that outlives the main program? They are supposed to be closed after the main exits, but there is a MSVC bug that needs to be fixed manually. – ark1974 Feb 18 '17 at 16:08
  • 1
    @ark1974: When control leaves the user-provided entry point (it's called `WinMain` in a GUI program, not `main`), it's mostly irrelevant, whether cleanup is performed or not. The process is about to exit, and the OS will reclaim all resources anyway. Not sure which *"bug"* you are referring to. – IInspectable Feb 18 '17 at 17:38

1 Answers1

0

If a main window window already exists you should destroy it before, using DestroyWindow. And no, it is not automatically destroyed when InitInstance is exited with FALSE. ExitInstance is executed, but existing windows will closed when the application exits.

Anyhow normales resources like files and memory are freed when the application exists.

xMRi
  • 14,982
  • 3
  • 26
  • 59