MFC, lost dialog pointer hWnd = ???

Dear all,

I am writting a MFC based program in VS2010 based on static libraries and in the linker with "No incremental" linking option(I add the code to the post).

The project has no problem in building in in VS 2010, and it actually shows the dialog window with the values in "virtual BOOL OnInitDialog() ". But when a run it, it seems that the CDialog is lost and it crashes. After debugging, i found out that the casted pointers for any of the elements of the windows, i.e. buttons, edit box or combobox, always gets a hWnd=???? value. For instance, at the moment i only want to write in pSerial->ShowWindow(False) which simply makes the program crash with an Assertion of "Access violation reading location 0x00000020."

In addition, I found out, also by debuging, that the actual pointers for pSERIAL, pCANCEL etc. have a meaningfull value before the program gets out from the constructor: "virtual BOOL OnInitDialog() ". It seems that the return true functions leads to "dlgcore.cpp", where the return shown below resets all values. I do not understand that to be honest.


Attached the code.

Any clues what is going wrong on my code?

MainCode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//-----------------------------------------------------------------------------------------
#include <afxwin.h>     // MFC core and standard components
#include "resource.h" // main symbols
//-----------------------------------------------------------------------------------------

#include <iostream>
#include <string>

using namespace std;

CEdit * pOUTPUT;
CEdit * pINPUT;
CButton * pSERIAL;
CButton * pCANCEL;
CComboBox * pCHOICE;

class SERIALIZER_FORM : public CDialog
{
   public:
   SERIALIZER_FORM(CWnd* pParent = NULL): CDialog(SERIALIZER_FORM::IDD, pParent)
   {    }

   // Dialog Data, name of dialog form
   enum{IDD = CW_Gen};

   protected:
   virtual void DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); }

   //Called right after constructor. Initialize things here.
   virtual BOOL OnInitDialog()
   {
                 // setting pointer to window objects
           CDialog::OnInitDialog();
                 CEdit * pOUTPUT = (CEdit *) GetDlgItem(CE_SHOW);
                 CEdit * pINPUT = (CEdit *) GetDlgItem(CE_BOX);
                 CComboBox * pCHOICE = (CComboBox *) GetDlgItem(CB_CHOOSE);
                 CButton * pSERIAL = (CButton *) GetDlgItem(CB_SERIAL);
                 CButton * pCANCEL = (CButton *) GetDlgItem(CB_CANCEL);
     
                 // Setting default values to Combo Box Menu
                 pCHOICE->AddString(L"msg1");
                 pCHOICE->AddString(L"msg2");

                 // Setting default values to Editing point
                 pINPUT->SetWindowText(L"Hi Y'all!");
                 pOUTPUT->SetWindowText(L"Your Output is shown here...");

           return true;
   }

public:
     DECLARE_MESSAGE_MAP()
afx_msg void show_buffer()
{
     //pOUTPUT =  SERIALIZER_FORM::GetDlgItem(CE_SHOW);
     //pOUTPUT->SetWindowText(L"serializador");
     //GetSafeHwnd();
     pSERIAL->ShowWindow(false);
}

afx_msg void OnCbnSelchangeChoose()
{
     
}



};

//-----------------------------------------------------------------------------------------

class TheSerializer : public CWinApp
{
public:
TheSerializer() {  }

public:
virtual BOOL InitInstance()
   {
  CWinApp::InitInstance();
  SERIALIZER_FORM dlg;
  m_pMainWnd = &dlg;
  INT_PTR nResponse = dlg.DoModal();
  return FALSE;

} //close function

  DECLARE_MESSAGE_MAP()
};

//-----------------------------------------------------------------------------------------
//Need a Message Map Macro for both CDialog and CWinApp

BEGIN_MESSAGE_MAP(SERIALIZER_FORM, CDialog)
     ON_COMMAND(CB_SERIAL, &SERIALIZER_FORM::show_buffer)
     ON_CBN_SELCHANGE(CB_CHOOSE, &SERIALIZER_FORM::OnCbnSelchangeChoose)

END_MESSAGE_MAP()

//-----------------------------------------------------------------------------------------

BEGIN_MESSAGE_MAP(TheSerializer, CWinApp)

END_MESSAGE_MAP()

//-----------------------------------------------------------------------------------------

TheSerializer theApp;  //Starts the Application




Resource.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by INTERFACE.rc
//
#define CW_Gen                          9
#define CE_BOX                          1001
#define CE_SHOW                         1002
#define CB_SERIAL                       1003
#define CB_CHOOSE                       1005
#define CS_INSTRUCT                     1007
#define CS_BYTESTREAM                   1008
#define CB_CANCEL                       1009

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        102
#define _APS_NEXT_COMMAND_VALUE         40001
#define _APS_NEXT_CONTROL_VALUE         1010
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif



This could be because you are confusing local and global variables:

You have these global variables:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

using namespace std;

CEdit * pOUTPUT;
CEdit * pINPUT;
CButton * pSERIAL;
CButton * pCANCEL;
CComboBox * pCHOICE;

class SERIALIZER_FORM : public CDialog
{



But in your OnInitDialog function - you declare and use some local variables


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
   //Called right after constructor. Initialize things here.
   virtual BOOL OnInitDialog()
   {
/**
ERROR HERE???
You now declare and use some local variables - which will be 
destroyed after this OnInitDialog function finishes - you probably meant to use
the Global variable you declared earlier??? **/                 

     // setting pointer to window objects
           CDialog::OnInitDialog();
                 CEdit * pOUTPUT = (CEdit *) GetDlgItem(CE_SHOW);
                 CEdit * pINPUT = (CEdit *) GetDlgItem(CE_BOX);
                 CComboBox * pCHOICE = (CComboBox *) GetDlgItem(CB_CHOOSE);
                 CButton * pSERIAL = (CButton *) GetDlgItem(CB_SERIAL);
                 CButton * pCANCEL = (CButton *) GetDlgItem(CB_CANCEL);
     
                 // Setting default values to Combo Box Menu
                 pCHOICE->AddString(L"msg1");
                 pCHOICE->AddString(L"msg2");

                 // Setting default values to Editing point
                 pINPUT->SetWindowText(L"Hi Y'all!");
                 pOUTPUT->SetWindowText(L"Your Output is shown here...");

           return true;
   }
You are right,

Deleting on OnInitDialog de Cedit * before pOUTPUT have solved the problem. My OnInitDialog now looks like the following one and it is executed with no problems.

Thanks

1
2
3
4
5
6
 CDialog::OnInitDialog();
               pOUTPUT = (CEdit *) GetDlgItem(CE_SHOW);
               pINPUT = (CEdit *) GetDlgItem(CE_BOX);
               pCHOICE = (CComboBox *) GetDlgItem(CB_CHOOSE);
               pSERIAL = (CButton *) GetDlgItem(CB_SERIAL);
               pCANCEL = (CButton *) GetDlgItem(CB_CANCEL);
Topic archived. No new replies allowed.