Closing dialog correctly
-
I have a dialog app that calls different dialog windows from a menu. Here is the problem: After opening a window from my menu , closing it, and then closing the application I get a memory leak detected error. This is how I establish the dialog: else if(PU_SELECT_ID == ID_PU_MALF_SI) { CMALFSIDlg *pdlg; error pts here->>>pdlg = new CMALFSIDlg; pdlg->Create(IDD_MALF_SI); pdlg->ShowWindow(SW_SHOW); } ???? So if I add a button to close this window how do I do so without having memory leaks? I've tried EndDialog() and DestroyWindow() but they don't seem to take care of the memory leak. Thanks for any help at alL!!!!
-
I have a dialog app that calls different dialog windows from a menu. Here is the problem: After opening a window from my menu , closing it, and then closing the application I get a memory leak detected error. This is how I establish the dialog: else if(PU_SELECT_ID == ID_PU_MALF_SI) { CMALFSIDlg *pdlg; error pts here->>>pdlg = new CMALFSIDlg; pdlg->Create(IDD_MALF_SI); pdlg->ShowWindow(SW_SHOW); } ???? So if I add a button to close this window how do I do so without having memory leaks? I've tried EndDialog() and DestroyWindow() but they don't seem to take care of the memory leak. Thanks for any help at alL!!!!
the 'new' keyword allocates a chunk of memory and puts a pointer to it.. your program wont clear out this memory like regular variables when you go out of scope.. after your done with the window just call
pdlg->DestroyWindow()
and thendelete pdlg
.. what you should prolly do instead is not use the 'new' keyword, this should do the same:CMALFSIDlg dlg;
pdlg.Create(IDD_MALF_SI);
pdlg.ShowWindow(SW_SHOW);that way you dont alocate the memory yourself, and the memory is handled by windows, and it will free up the memory on its own.. yeah im a newbie, but im tryin! -dz
-
the 'new' keyword allocates a chunk of memory and puts a pointer to it.. your program wont clear out this memory like regular variables when you go out of scope.. after your done with the window just call
pdlg->DestroyWindow()
and thendelete pdlg
.. what you should prolly do instead is not use the 'new' keyword, this should do the same:CMALFSIDlg dlg;
pdlg.Create(IDD_MALF_SI);
pdlg.ShowWindow(SW_SHOW);that way you dont alocate the memory yourself, and the memory is handled by windows, and it will free up the memory on its own.. yeah im a newbie, but im tryin! -dz
I don't this approach will do any good. The reason is the dialog is created on the stack, which you stated correctly. However, since it's created on the stack it will also be destroyed when the function exits. So you'll never see the dialog. Do a search for a lesson on multiple dialogs, but it amounts to catching the "close" messages and calling 'delete' on the right pointer. This will free the memory allocated (like you said).
-
I have a dialog app that calls different dialog windows from a menu. Here is the problem: After opening a window from my menu , closing it, and then closing the application I get a memory leak detected error. This is how I establish the dialog: else if(PU_SELECT_ID == ID_PU_MALF_SI) { CMALFSIDlg *pdlg; error pts here->>>pdlg = new CMALFSIDlg; pdlg->Create(IDD_MALF_SI); pdlg->ShowWindow(SW_SHOW); } ???? So if I add a button to close this window how do I do so without having memory leaks? I've tried EndDialog() and DestroyWindow() but they don't seem to take care of the memory leak. Thanks for any help at alL!!!!
iF you are using MFC, before the program exits, you'll go into the app's
ExitInstance
function. Assuming you store a pointer to this dialog in the app class (or can get at it from there), you should call this in theExitInstance
function:delete pdlg;
(Note: to be robust, you should ensure that either pdlg points to a valid dialog, or to NULL!) Even if you win the rat race, you're still a rat. -
I have a dialog app that calls different dialog windows from a menu. Here is the problem: After opening a window from my menu , closing it, and then closing the application I get a memory leak detected error. This is how I establish the dialog: else if(PU_SELECT_ID == ID_PU_MALF_SI) { CMALFSIDlg *pdlg; error pts here->>>pdlg = new CMALFSIDlg; pdlg->Create(IDD_MALF_SI); pdlg->ShowWindow(SW_SHOW); } ???? So if I add a button to close this window how do I do so without having memory leaks? I've tried EndDialog() and DestroyWindow() but they don't seem to take care of the memory leak. Thanks for any help at alL!!!!
In the dialog, add a handler for OnNCDestroy. Inside that, do
delete this;
This might work. It has been a while. Tim Smith "Programmers are always surrounded by complexity; we can not avoid it... If our basic tool, the language in which we design and code our programs, is also complicated, the language itself becomes part of the problem rather that part of the solution." Hoare - 1980 ACM Turing Award Lecture
-
In the dialog, add a handler for OnNCDestroy. Inside that, do
delete this;
This might work. It has been a while. Tim Smith "Programmers are always surrounded by complexity; we can not avoid it... If our basic tool, the language in which we design and code our programs, is also complicated, the language itself becomes part of the problem rather that part of the solution." Hoare - 1980 ACM Turing Award Lecture
I've tried doing the deletes and destroys, but it just doesn't work. I have the pointers defind under the OnMouseBtnClick and when I go to delete VC says that it is undefined. I'm trying to find the cleanest and most modular way to do this, am I going to have to define these pointer ahead of the menu call making them global to the rest of the code?