I have an MFC dialog, which is called Dlg1
myobject* Dlg1 = new myobject();
This dialog has a parent dialog called A; I have a function in A, which is called on closing:
A::Destroy()
{
if(Dlg1 )
delete Dlg1; // this is triggering `DebugBreak(); here i get A.exe has triggered a breakpoint,
// the rest of the code
}
If I close Dlg1
dialog, manually by clicking on the close button, then close the main dialog A
, then everything is fine no problem. However, if Dlg1
is running and I close A, then I get DebugBreak()
. This issue occurs only in debug mode, in the release mode no problem.
If we are to assume that the code you posted in your question is the actual code you're using, the issue may be this:
Note that
Dlg1
is a local variable, not a member variable. Then you have this:The
Dlg1
in the code above is the member variableDlg1
of class or structA
. It more than likely was never initialized, due to your code creating a dynamic object locally by mistake. Issuing adelete
on an uninitialized pointer will wreak havoc.If this is the issue, then to fix it, one suggestion is to have somewhere:
where
SomeFunction
would be another member function ofA
.