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:
myobject* Dlg1 = new myobject();
Note that Dlg1
is a local variable, not a member variable. Then you have this:
A::Destroy()
{
if (Dlg1 )
delete Dlg1;
}
The Dlg1
in the code above is the member variable Dlg1
of class or struct A
. It more than likely was never initialized, due to your code creating a dynamic object locally by mistake. Issuing a delete
on an uninitialized pointer will wreak havoc.
If this is the issue, then to fix it, one suggestion is to have somewhere:
A::SomeFuntion()
{
//...
Dlg1 = new myobject(); // set the member variable Dlg1, not a local variable.
//...,
}
where SomeFunction
would be another member function of A
.