Game Development Community

Error associating variable with edit box in VC++

by Jason Fultz · in Technical Issues · 10/09/2001 (3:12 pm) · 6 replies

I'm having a problem associating a new variable with a text (edit) box on one of my forms. Here's exactly what is happening:

I create a new Edit Box on my form. Let's call it IDC_EDIT1. I compile the application. No errors. I run the application. Again, no errors, but of course I am unable to really do anything with the text box until a variable is associated with it.

Then, I used Class Wizard->Member Variables and selected the IDC_EDIT1 control. I clicked on the Add Variable button and used the following:

Member variable name: m_Edit1
Category: Value
Variable type: UINT

I then click on Ok, Ok, and then I'm out of the Wizard. So what I just did was exactly as I was taught to associate a variable with a control. I go back to the Wizard and inspect my new variable, and all looks fine.

I compile and then:
I get an unhandled exception error. I go to debug it and an Access Violation error pops up. The program stops on this line of the following code:
m_pMainWnd = &dlg;


#include "stdafx.h"
#include "DnD.h"
#include "DnDDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

BEGIN_MESSAGE_MAP(CDnDApp, CWinApp)
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////
// CDnDApp construction

CDnDApp::CDnDApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////
// The one and only CDnDApp object

CDnDApp theApp;

/////////////////////////////////////////////////////////
// CDnDApp initialization

BOOL CDnDApp::InitInstance()
{
AfxEnableControlContainer();

#ifdef _AFXDLL
Enable3dControls();
#else
Enable3dControlsStatic();
#endif

CDnDDlg dlg;

// This is where the debugger stops!
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
}
else if (nResponse == IDCANCEL)
{
}

return FALSE;
}

Now, the really weird part is this. Before I added a new Edit Box, another Edit is already on the form. And it already has an associated variable with it. I did this a few months ago, as I just started looking at this program again the other day. I've looked at all references to the original Edit Box and compared them with references to the new Edit Box, and they all match up. This new edit box is nearly identical to the original one. I can run the application with no problems as long as the new edit box isn't in the program but the the original edit box doesn't give me a problem.

Does anyone have any ideas as to what is happening here and how to fix it? I really need to be able to add Edit Boxes to my program. Otherwise, what's the point in using VC++?

Thanks beforehand!

About the author

Recent Threads


#1
10/11/2001 (1:58 am)
It's difficult to see where the problem is from the above. If you have a way of posting the entire project on the internet, it would help debugging.

Otherwise, the problem seems to come from your dialogue during creation at the line CDnDDlg dlg; Thus the code you need to show is the code for the dialogue DnDDlg.cpp and DnDDlg.h (the form DnD.rc itself could be helpfull).

Debuggging Access Violations can be tricky, but in this case it should be fairly simple. One trick is to use the call stack to see where the last function called was. This can sometimes be corrupted by the crash itself, but it your case I would doubt this.

Try debugging the constructor for the dialogue for extra info, though this may not show your problem.

Good luck - I'll keep an eye on this thread to see if I can help.

Cheers,

Doug. EnkiSoftware Limited
#2
10/11/2001 (4:04 am)
My guess is that somehow the resource file (.rc file) has become corrupted. Its failing to create the Dialog box, which is normally constructed by the dialog creation code reading the resource template and trying to construct the items from thier resource description.

take a look in the .rc file (its just a text file with descriptions of your dialog stuff).

Also, as a test, make a new project and place 2 edit controls with member variables in em. Just to make sure that actually DOES work (if it doesnt VC is screwed somehow).

Phil.
#3
10/11/2001 (4:36 am)
Phil - I'm not sure this is the answer. The .rc file does not actually contain the variables for controls, just the definitions of the controls. Since The program works with the edit box in it, but not with the member variable 'joined' to that edit box then the problem is with the dialogue .cpp and/or .h files.

In these files, you will find code such as:

.h file--->
//{{AFX_DATA(CDnDDlg)
UINT m_Edit1;
//}}AFX_DATA

.cpp file--->
CDnDDlg::CDnDDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDnDDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDnDDlg)
m_Edit1 = 0;
//}}AFX_DATA_INIT
}
void CDnDDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDnDDlg)
DDX_????(pDX, IDC_EDIT1, m_Edit1);
//}}AFX_DATA_MAP
}

Note that DDX_??? should be something like DDX_Uint but as I don't have Visual C++ here I can't remember the name. I'll look it up soon.

Good luck!
#4
10/11/2001 (5:41 am)
The correct DDX function (see above post) is

void AFXAPI DDX_Text( CDataExchange* pDX, int nIDC, UINT& value );

as the EditBox has text, which needs to be converted to UINT. Hope that helps.
#5
10/11/2001 (10:40 am)
Thanks guys...

I'm sure you'll believe it when I tell you what has happened since I posted this problem, as you've probably seen things like this numerous times while using VC++.

The day I posted the problem, I added that Edit Box and associated the m_Edit1 variable to it just as I've been taught and as someone else instructed me to do when I asked about this problem (same thing I described in the original post). At the time, the problem was repeated.

Later that night, after rebooting, and after a few more changes to a different part of the code, it started working without error. It's as if something was just corrupt. I don't even know what I did to fix it.

So this goes down in my book of problems with VC++ that I'll never know a solution to until it happens again.

But I appreciate all your help with this. It's nice to have a forum that I can go to if something like this happens again.

Man, sometimes VC++ just really frustrates me. I guess with more experience with the language, I'll begin to understand exactly what is causing these kinds of strange problems.

Thanks again!
Jason...
#6
10/11/2001 (10:44 am)
a programmer's best friend at times is the God of FM. ;)