简体   繁体   中英

strange behavior with MFC's CString

I am getting the strangest memory behavior using an MFC CString:

line 138: PCD_Dir = m_pRunPCD->m_PCD_Dir;

declarations:

CString PCD_Dir; (file scope, defined as a global outside of CServer )

class CRunPCD {

public:
CString m_PCD_Dir;
}

call stack:

Support.dll!CServer::Init(CCfcController * pv=0x007ebe78)  Line 138 C++
Support.dll!CCfcController::OnInitialUpdate()  Line 156 + 0xf bytes C++
Support.dll!CCfcApp::CCncApp()  Line 140    C++
Support.dll!newInstance()  Line 162 + 0x2c bytes    C++

memory (from watch window):

(wchar_t**)&(m_pRunPCD->m_PCD_Dir): 
0x007ec270  wchar_t * *

m_pRunPCD->m_PCD_Dir.m_pszData:
0x007ee8a0 "C:\ProgramData\WAI\PC-DMIS\"    wchar_t *

(wchar_t**)&PCD_Dir:
0x068f4698 class ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > PCD_Dir    wchar_t * *

PCD_Dir.m_pszData:
0x789cb8f8 ""   wchar_t *

I then step into the assignment and my environment looks like this:

code:

// Assignment operators
CStringT& operator=(_In_ const CStringT& strSrc)
{
CThisSimpleString::operator=( strSrc );
return( *this );
}

call stack:

mfc100ud.dll!ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >::operator=(const ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > & strSrc=<Bad Ptr>)  Line 1218    C++
Support.dll!CServer::Init(CCfcController * pv=0x007ebe78)  Line 141 C++
Support.dll!CCfcController::OnInitialUpdate()  Line 156 + 0xf bytes C++
Support.dll!CCfcApp::CCncApp()  Line 140    C++
Support.dll!newInstance()  Line 162 + 0x2c bytes    C++

memory:

&strSrc 
0x007ec269 const ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > *

strSrc.m_pszData:
0xff000000 <Bad Ptr>    wchar_t *

so for some reason the value is not getting passed correctly...? interestingly, if look in this frame:

CNCSupport.dll!CServer::Init(CCncController * pv=0x007ebe78)  Line 141  C++

then the value is correct. any idea what the problem is?! i am completely stumped. any help would be greatly appreciated! thanks.

Instead of:

PCD_Dir = m_pRunPCD->m_PCD_Dir;

try:

PCD_Dir = (LPCTSTR) m_pRunPCD->m_PCD_Dir;

Turns out that the address of strSrc and m_pRunPCD->m_PCD_Dir were offset by 1, which was the reason the buffer pointer was invalid. turns out I had incorrectly enabled packing in a header file to resolve another issue which led to this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM