MFC application using /clr written in VS2010. Multi-threaded DLL (/MD) run-time library. Problem occurs when I switch the Preprocessor Definition of NDEBUG to _DEBUG. NDEBUG disables the assertion which pops up when _DEBUG is defined. Am I doing something wrong here in managing the creation and deletion of a class pointer?
Once I make the switch from NDEBUG to _DEBUG, I get a "_Block_Type_Is_Valid (pHead->nBlockUse)" assertion failed error at run time.
Class A : "A.h"
#include "B.h"
class A
{
public:
A(void);
~A(void);
A(const A&);
A& operator=(const A&);
B* p_B;
};
Class A: "A.cpp"
#include "StdAfx.h"
#include "A.h"
A::A(void)
{
p_B = new B();
}
A::~A(void)
{
delete p_B;
}
// 1. copy constructor
A::A(const A& that)
{
p_B = new B();
*p_B = *that.p_B;
}
// 2. copy assignment operator
A& A::operator=(const A& that)
{
*p_B = *that.p_B;
return *this;
}
Class B: "B.h"
class B
{
public:
B(void);
~B(void);
B(const B&);
B& operator=(const B&);
};
Class B: "B.cpp"
#include "StdAfx.h"
#include "B.h"
B::B(void) { }
B::~B(void) { }
// 1. copy constructor
B::B(const B& that)
{
}
// 2. copy assignment operator
B& B::operator=(const B& that)
{
return *this;
}
ModalDlg.cpp (Where an object of class A is instantiated)
BOOL CTestingReleaseBuildDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
A a;
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
Then I simply instantiate the A class in my MFC dialog and that causes the assertion to fail. My question is, "Am I doing something wrong here in the creation and deletion of a class pointer?" The assertion specifically fails in the "delete p_B" instruction of class A's destructor.
EDIT:
I instantiate the A class with BOOL CMyMFCClassDLG::OnInitDialog() { ... A a; ...}
EDIT2: I defined the copy constructors and the copy assignment operators for both A and B classes. They are never invoked.
EDIT3: Worth mentioning that if I remove the delete p_B;
statement in A's destructor then no more assertion failure.
EDIT4: The program runs fine when in Debug mode with /MDd and _DEBUG defined. The assertion fails when I run in Release mode with /MD and _DEBUG. Which I think might be causing the issue since /MD should probably be ran with NDEBUG.
EDIT5: I updated the code as suggested by @Christophe and inserted the function of where an object of Class A is instantiated. I dont want to copy/paste the rest of the Modal Dialog application but you can replicate the exact code by starting a new Modal Dialog based MFC application in VS2010 and change the project configuration to work with /CLR mode, set the Run-Time Library to /MD and include the _DEBUG keyword in pre-processor definitions field.
EDIT6: Link to project https://drive.google.com/drive/folders/1q0n9c6yMZ2ZKnakH6Z5NbVeGsAWfUAc1?usp=sharing