• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 456
  • Last Modified:

Memory Leaks

I have a dialog box with two exit options.

In the first, I free all memory taken by my operations, then call CDialog::OnOk(). It passes smoothly and with no problems.

In the second, I free all memory taken, just as above and with the same commands, and then call exit(0). I need this because this is a 2nd-level dialog and I want to give the user the opportunity to quit the application immediately, without having to return him to the calling 1st-level dialog.

However, this exit(0) causes a memory leak: nothing serious, less than 100 bytes but still it's a nuisance. So, the question is actually two questions:

1) What does CDialog::OnOk() do to free memory that exit(0) doesn't do?

2) Is there any Windows-style function to replace the DOS-style exit(0) function?
1 Solution
You can not call exit() within your MFC application without getting in trouble.

CDIalog's destructor is doing all the work need to free up memory used etc. Exit() will terminate the program immediately without any kind of work (let's say so but actualy exit() is doing some work but not regarding MFC objects because it know nothing about them!).

You can close your application this way:

if you have CMainFrame derived main window i.e. Doc.View model tehn you  can use this
AfxGetMainWnd()->SendMessage(WM_COMMAND, (WPARAM)ID_APP_EXIT);
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now