I do not understand what I'm doing wrong. Trying to create a dialog using MFC as follows:

There is a menu item and a dialogue (resource.h):

... #define ID_BULK_ADD_DEPEND_CNTRS_MUNU 40056 #define IDD_BULK_ADD_DEPEND_CNTRS 1078 ... 

That "join" like this (MainFrame.h):

 BEGIN_MESSAGE_MAP(CMainFrame,CFrameWnd) ... ON_COMMAND(ID_BULK_ADD_DEPEND_CNTRS_MUNU,OnBulkAddDependCntrs) ... END_MESSAGE_MAP() 

And actually OnBulkAddDependCntrs is defined like this:

 void CMainFrame::OnBulkAddDependCntrs() { 小BulkAddDependCntrs* ptr_BADC_dialog = new 小BulkAddDependCntrs(m_hWnd); if (ptr_BADC_dialog != nullptr){ BOOL ret = ptr_BADC_dialog->Create(ptr_BADC_dialog->IDD, m_hWnd); #ifdef _DEBUG if (!ret) AfxMessageBox(_T("Error creating 小BulkAddDependCntrs (IDD_BULK_ADD_DEPEND_CNTRS)")); #endif ptr_BADC_dialog->ShowWindow(SW_SHOW); } #ifdef _DEBUG else AfxMessageBox(_T("Error creating object 小BulkAddDependCntrs (IDD_BULK_ADD_DEPEND_CNTRS)")); #endif } 

Defining a dialog class:

 #pragma once #include "Resource.h" // 小BulkAddDependCntrs dialog class 小BulkAddDependCntrs : public CDialog { DECLARE_DYNAMIC(小BulkAddDependCntrs) public: 小BulkAddDependCntrs(CWnd* pParent = NULL); // standard constructor virtual ~小BulkAddDependCntrs(); virtual BOOL OnInitDialog(); virtual BOOL Create(UINT nIDTemplate, CWnd *pParent); // Dialog Data enum { IDD = IDD_BULK_ADD_DEPEND_CNTRS }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support DECLARE_MESSAGE_MAP() }; 

And its implementation:

 #include "stdafx.h" #include "Counters.h" #include "BulkAddDependCntrs.h" // 小BulkAddDependCntrs dialog IMPLEMENT_DYNAMIC(小BulkAddDependCntrs, CDialog) 小BulkAddDependCntrs::小BulkAddDependCntrs(CWnd* pParent /*=NULL*/) : CDialog(小BulkAddDependCntrs::IDD, pParent) { #ifdef _DEBUG OutputDebugString("小BulkAddDependCntrs::constructer()\n"); #endif } 小BulkAddDependCntrs::~小BulkAddDependCntrs() { } void 小BulkAddDependCntrs::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BOOL 小BulkAddDependCntrs::OnInitDialog() { #ifdef _DEBUG OutputDebugString("小BulkAddDependCntrs::OnInitDialog()\n"); #endif return CDialog::OnInitDialog(); } BOOL 小BulkAddDependCntrs::Create(UINT nIDTemplate, CWnd *pParent) { #ifdef _DEBUG OutputDebugString("小BulkAddDependCntrs::Create()\n"); #endif return CDialog::Create(nIDTemplate, pParent); } BEGIN_MESSAGE_MAP(小BulkAddDependCntrs, CDialog) END_MESSAGE_MAP() // 小BulkAddDependCntrs message handlers 

As a result, the debager writes something like this:

 小BulkAddDependCntrs::constructer() 小BulkAddDependCntrs::Create() Detected memory leaks! Dumping objects -> {47718} client block at 0x00EE7C58, subtype c0, 116 bytes long. a 小BulkAddDependCntrs object at $00EE7C58, 116 bytes long {47666} normal block at 0x00EE7848, 42 bytes long. Data: <0 H > 30 82 48 01 19 00 00 00 19 00 00 00 01 00 00 00 {47665} normal block at 0x00EE77F0, 24 bytes long. Data: <0 H > 30 82 48 01 07 00 00 00 07 00 00 00 01 00 00 00 {47664} normal block at 0x00EE7798, 26 bytes long. Data: <0 H > 30 82 48 01 09 00 00 00 09 00 00 00 01 00 00 00 {47663} normal block at 0x00EE7748, 20 bytes long. Data: <0 H > 30 82 48 01 03 00 00 00 03 00 00 00 01 00 00 00 {47662} normal block at 0x00EE76F0, 23 bytes long. Data: <0 H > 30 82 48 01 06 00 00 00 06 00 00 00 01 00 00 00 {47661} normal block at 0x00EE7698, 25 bytes long. Data: <0 H > 30 82 48 01 08 00 00 00 08 00 00 00 01 00 00 00 {47660} normal block at 0x00EE75F8, 96 bytes long. Data: < vv > A8 76 EE 00 76 00 00 00 02 00 00 00 00 CD CD CD {39028} normal block at 0x05D50040, 3846392 bytes long. Data: < 24805821> C0 D8 04 00 03 00 00 00 32 34 38 30 35 38 32 31 {277} normal block at 0x00EE8688, 4176 bytes long. Data: < > F2 FD E4 E3 00 FE FE FE D2 E5 EF EB EE FD ED E5 {242} normal block at 0x00EE7598, 30 bytes long. Data: <0 H > 30 82 48 01 0D 00 00 00 0D 00 00 00 01 00 00 00 {241} normal block at 0x00EE7530, 42 bytes long. Data: <0 H > 30 82 48 01 19 00 00 00 19 00 00 00 01 00 00 00 {240} normal block at 0x00EE74D8, 26 bytes long. Data: <0 H > 30 82 48 01 09 00 00 00 09 00 00 00 01 00 00 00 {239} normal block at 0x00EE7480, 23 bytes long. Data: <0 H > 30 82 48 01 06 00 00 00 06 00 00 00 01 00 00 00 {238} normal block at 0x00EE7428, 26 bytes long. Data: <0 H > 30 82 48 01 09 00 00 00 09 00 00 00 01 00 00 00 {237} normal block at 0x00EE73D0, 25 bytes long. Data: <0 H > 30 82 48 01 08 00 00 00 08 00 00 00 01 00 00 00 {236} normal block at 0x00EE2550, 18 bytes long. Data: <0 H > 30 82 48 01 01 00 00 00 01 00 00 00 01 00 00 00 {235} normal block at 0x00EE24B0, 96 bytes long. Data: <`% > 60 25 EE 00 14 00 00 00 00 00 00 00 00 CD CD CD {234} normal block at 0x00EE2468, 8 bytes long. Data: < > 00 00 00 00 00 00 00 00 {232} normal block at 0x00EE7380, 20 bytes long. Data: <0 H > 30 82 48 01 03 00 00 00 03 00 00 00 01 00 00 00 {231} normal block at 0x00EE7328, 21 bytes long. Data: <0 H > 30 82 48 01 04 00 00 00 04 00 00 00 01 00 00 00 {230} normal block at 0x00EE72D8, 20 bytes long. Data: <0 H > 30 82 48 01 03 00 00 00 03 00 00 00 01 00 00 00 {229} normal block at 0x00EE25A0, 21 bytes long. Data: <0 H > 30 82 48 01 04 00 00 00 04 00 00 00 01 00 00 00 {225} normal block at 0x00EE2328, 100 bytes long. Data: < @ > 00 00 00 00 40 01 00 00 00 01 00 08 00 00 00 00 f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\plex.cpp(29) : {224} normal block at 0x00EE2210, 124 bytes long. Data: < q > 00 00 00 00 00 00 00 00 00 00 00 00 00 71 EE 00 {221} normal block at 0x00EE2080, 25 bytes long. Data: <0 H > 30 82 48 01 08 00 00 00 08 00 00 00 01 00 00 00 .\CountersApp.cpp(250) : {220} client block at 0x00EE6F70, subtype c0, 808 bytes long. a CFrameWnd object at $00EE6F70, 808 bytes long {219} normal block at 0x00EE22D0, 23 bytes long. Data: <0 H > 30 82 48 01 06 00 00 00 06 00 00 00 01 00 00 00 {81} normal block at 0x00EE6F20, 20 bytes long. Data: <0 H > 30 82 48 01 03 00 00 00 03 00 00 00 02 00 00 00 {65} normal block at 0x00EE1C90, 21 bytes long. Data: <0 H > 30 82 48 01 04 00 00 00 04 00 00 00 01 00 00 00 {64} normal block at 0x00EE1C30, 29 bytes long. Data: < > C0 D0 CC 20 E4 EB FF 20 F0 E0 E1 EE F2 FB 20 F1 Object dump complete. The thread 'Win32 Thread' (0xcb0) has exited with code 3 (0x3). The thread 'Win32 Thread' (0x1138) has exited with code 3 (0x3). The thread 'Win32 Thread' (0xc68) has exited with code 3 (0x3). The thread 'Win32 Thread' (0x5b8) has exited with code 3 (0x3). The thread 'Win32 Thread' (0x568) has exited with code 3 (0x3). The thread 'Win32 Thread' (0x1820) has exited with code 3 (0x3). The thread 'Win32 Thread' (0x1354) has exited with code 3 (0x3). The thread 'Win32 Thread' (0x1abc) has exited with code 3 (0x3). The program '[6252] Counters.exe: Native' has exited with code 3 (0x3). 

That corresponds, as far as I understand, to the fact that the object was created, but the call to Creat () leads to a memory leak.

Naturally, no dialog box opens.

First encountered MFC. Any idea how to solve the problem?

    1 answer 1

    小BulkAddDependCntrs * ptr_BADC_dialog = new 小BulkAddDependCntrs (m_hWnd); You made new, at the end you should delete the dynamically created object, preferably with a check.

     if (ptr_BADC_dialog !=nullptr) { delete ptr_BADC_dialog; ptr_BADC_dialog = nullptr; }