accidentprone
asked on
CString Access Violation mystery
Help me figure out how to fix this!
I'm trying to pass a CString object from a message handler to another function that sets up a file name for opening.
My app has a runtime error at the point where the assignment is made.
When I debug, I go here:
in ...\mfc\include\afx.inl:
__________________________ __________ __
// CString
_AFX_INLINE CStringData* CString::GetData() const
{ ASSERT(m_pchData != NULL); return ((CStringData*)m_pchData)- 1; }
_AFX_INLINE void CString::Init()
__________________________ __________ _____
the call before that was from ...\mfc\src\strcore.cpp:
__________________________ __________ __________ ____
void CString::AllocBeforeWrite( int nLen)
{
if (GetData()->nRefs > 1 || nLen > GetData()->nAllocLength)
{
Release();
AllocBuffer(nLen);
}
ASSERT(GetData()->nRefs <= 1);
}
__________________________ __________ ________
My code looks like this:
in the header:
__________________________ __________ ___
class CFe517App : public CWinApp
{
public:
CFe517App();
void SetTutName (LPCTSTR);
private:
CString m_TutorName;
__________________________ __________ _
in the cpp file:
__________________________ __________ __________ ____
void CFe517App::SetTutName ( LPCTSTR TName)
{
m_TutorName = TName; // this breaks!!
}
__________________________ __________ __________ _____
I've tried this with m_TutorName as both public and private.
Please help me see what I'm doing wrong!
I'm trying to pass a CString object from a message handler to another function that sets up a file name for opening.
My app has a runtime error at the point where the assignment is made.
When I debug, I go here:
in ...\mfc\include\afx.inl:
__________________________
// CString
_AFX_INLINE CStringData* CString::GetData() const
{ ASSERT(m_pchData != NULL); return ((CStringData*)m_pchData)-
_AFX_INLINE void CString::Init()
__________________________
the call before that was from ...\mfc\src\strcore.cpp:
__________________________
void CString::AllocBeforeWrite(
{
if (GetData()->nRefs > 1 || nLen > GetData()->nAllocLength)
{
Release();
AllocBuffer(nLen);
}
ASSERT(GetData()->nRefs <= 1);
}
__________________________
My code looks like this:
in the header:
__________________________
class CFe517App : public CWinApp
{
public:
CFe517App();
void SetTutName (LPCTSTR);
private:
CString m_TutorName;
__________________________
in the cpp file:
__________________________
void CFe517App::SetTutName ( LPCTSTR TName)
{
m_TutorName = TName; // this breaks!!
}
__________________________
I've tried this with m_TutorName as both public and private.
Please help me see what I'm doing wrong!
Where is your message handler?
I'd like to know what TName is when it enters the function..can you put a TRACE in and detail the result?
Your TName may be NULL or corrupted pointer. Check it is value in debuger.
ASKER
Running the debugger, I get that TName = 0x0042276c "stat.mwd", when I begin SetTutName, so that is correct. I got kiched out to
...\mfc\src\STRCORE.cpp, like this:
__________________________ ___
void CString::AllocBeforeWrite( int nLen)
{
if (GetData()->nRefs > 1 || nLen > GetData()->nAllocLength) // breaking here!
{
Release();
AllocBuffer(nLen);
}
ASSERT(GetData()->nRefs <= 1);
}
__________________________ ________
and the debugger tells me this:
- this 0x0012fb58 {""}
- m_pchData 0x00000000 ""
CXX0030: Error: expression cannot be evaluated
nLen 8
If I try to F10 or F11 the function, I get a message box saying:
"Unhandled exception in fe517.exe (MFC42D.DLL): 0xC0000005: Access Violation"
Also, galkin, my message handler is in another file: it looks like this:
headerinfo:
__________________________ __________ _______
class CTutorDlg : public CDialog
{
// Construction
public:
CTutorDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CTutorDlg)
enum { IDD = IDD_TUTORIALDLG };
CString m_TutName;
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTutorDlg)
protected:
virtual void DoDataExchange(CDataExchan ge* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CTutorDlg)
virtual void OnOK();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
and the implementation looks like this:
__________________________ __________ _______
void CTutorDlg::OnOK()
{
CDialog::OnOK();
AfxMessageBox (m_TutName);
CFe517App* pApp;
pApp -> SetTutName (m_TutName);
}
...\mfc\src\STRCORE.cpp, like this:
__________________________
void CString::AllocBeforeWrite(
{
if (GetData()->nRefs > 1 || nLen > GetData()->nAllocLength) // breaking here!
{
Release();
AllocBuffer(nLen);
}
ASSERT(GetData()->nRefs <= 1);
}
__________________________
and the debugger tells me this:
- this 0x0012fb58 {""}
- m_pchData 0x00000000 ""
CXX0030: Error: expression cannot be evaluated
nLen 8
If I try to F10 or F11 the function, I get a message box saying:
"Unhandled exception in fe517.exe (MFC42D.DLL): 0xC0000005: Access Violation"
Also, galkin, my message handler is in another file: it looks like this:
headerinfo:
__________________________
class CTutorDlg : public CDialog
{
// Construction
public:
CTutorDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CTutorDlg)
enum { IDD = IDD_TUTORIALDLG };
CString m_TutName;
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTutorDlg)
protected:
virtual void DoDataExchange(CDataExchan
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CTutorDlg)
virtual void OnOK();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
and the implementation looks like this:
__________________________
void CTutorDlg::OnOK()
{
CDialog::OnOK();
AfxMessageBox (m_TutName);
CFe517App* pApp;
pApp -> SetTutName (m_TutName);
}
ASKER
Adjusted points to 78
ASKER
I also want to mention that if there is a better or easier way to be doing this, that will definitely suffice for an answer.
The idea here is to get a string (CString) out of a CComboBox and into a function somewhere else that can use it.
I'd be grateful if you told me how YOU'd do such a task...see, I even ADDED 48 POINTS!
The idea here is to get a string (CString) out of a CComboBox and into a function somewhere else that can use it.
I'd be grateful if you told me how YOU'd do such a task...see, I even ADDED 48 POINTS!
ASKER
I also want to mention that if there is a better or easier way to be doing this, that will definitely suffice for an answer.
The idea here is to get a string (CString) out of a CComboBox and into a function somewhere else that can use it.
I'd be grateful if you told me how YOU'd do such a task...see, I even ADDED 48 POINTS!
The idea here is to get a string (CString) out of a CComboBox and into a function somewhere else that can use it.
I'd be grateful if you told me how YOU'd do such a task...see, I even ADDED 48 POINTS!
ASKER
Well, I think I need to change my question, because I have figured out what I did wrong, but I don't know yet how to do it right.
I think I was getting a runtime error because, in the message handler, I was creating a pointer to a new copy of the calling object, and not the calling object itself.
I have overridden OnOK() to return a string, but I don't know how to grab hold of it before the dialog box exits.
Thanks for reading this far!
I think I was getting a runtime error because, in the message handler, I was creating a pointer to a new copy of the calling object, and not the calling object itself.
I have overridden OnOK() to return a string, but I don't know how to grab hold of it before the dialog box exits.
Thanks for reading this far!
ASKER
Well, I think I need to change my question, because I have figured out what I did wrong, but I don't know yet how to do it right.
I think I was getting a runtime error because, in the message handler, I was creating a pointer to a new copy of the calling object, and not the calling object itself.
I have overridden OnOK() to return a string, but I don't know how to grab hold of it before the dialog box exits.
Thanks for reading this far!
I think I was getting a runtime error because, in the message handler, I was creating a pointer to a new copy of the calling object, and not the calling object itself.
I have overridden OnOK() to return a string, but I don't know how to grab hold of it before the dialog box exits.
Thanks for reading this far!
ASKER
Well, I think I need to change my question, because I have figured out what I did wrong, but I don't know yet how to do it right.
I think I was getting a runtime error because, in the message handler, I was creating a pointer to a new copy of the calling object, and not the calling object itself.
I have overridden OnOK() to return a string, but I don't know how to grab hold of it before the dialog box exits.
Thanks for reading this far!
I think I was getting a runtime error because, in the message handler, I was creating a pointer to a new copy of the calling object, and not the calling object itself.
I have overridden OnOK() to return a string, but I don't know how to grab hold of it before the dialog box exits.
Thanks for reading this far!
ASKER
Well, I think I need to change my question, because I have figured out what I did wrong, but I don't know yet how to do it right.
I think I was getting a runtime error because, in the message handler, I was creating a pointer to a new copy of the calling object, and not the calling object itself.
I have overridden OnOK() to return a string, but I don't know how to grab hold of it before the dialog box exits.
Thanks for reading this far!
I think I was getting a runtime error because, in the message handler, I was creating a pointer to a new copy of the calling object, and not the calling object itself.
I have overridden OnOK() to return a string, but I don't know how to grab hold of it before the dialog box exits.
Thanks for reading this far!
ASKER
Well, I think I need to change my question, because I have figured out what I did wrong, but I don't know yet how to do it right.
I think I was getting a runtime error because, in the message handler, I was creating a pointer to a new copy of the calling object, and not the calling object itself.
I have overridden OnOK() to return a string, but I don't know how to grab hold of it before the dialog box exits.
Thanks for reading this far!
I think I was getting a runtime error because, in the message handler, I was creating a pointer to a new copy of the calling object, and not the calling object itself.
I have overridden OnOK() to return a string, but I don't know how to grab hold of it before the dialog box exits.
Thanks for reading this far!
ASKER
Well, I think I need to change my question, because I have figured out what I did wrong, but I don't know yet how to do it right.
I think I was getting a runtime error because, in the message handler, I was creating a pointer to a new copy of the calling object, and not the calling object itself.
I have overridden OnOK() to return a string, but I don't know how to grab hold of it before the dialog box exits.
Thanks for reading this far!
I think I was getting a runtime error because, in the message handler, I was creating a pointer to a new copy of the calling object, and not the calling object itself.
I have overridden OnOK() to return a string, but I don't know how to grab hold of it before the dialog box exits.
Thanks for reading this far!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This was very good, but because I wanted to get the string out of
the dialog box, the code I ended up using looked like this:
BOOL CFe517App::OnFileViewTutor ials()
{
CTutorDlg tutordlg;
if (tutordlg.DoModal () == IDOK)
{
m_TutorName = tutordlg.m_TutName;
CWinApp::OpenDocumentFile (m_TutorName);
return true;
}
return false;
}
thank you very much!!!
the dialog box, the code I ended up using looked like this:
BOOL CFe517App::OnFileViewTutor
{
CTutorDlg tutordlg;
if (tutordlg.DoModal () == IDOK)
{
m_TutorName = tutordlg.m_TutName;
CWinApp::OpenDocumentFile (m_TutorName);
return true;
}
return false;
}
thank you very much!!!