简体   繁体   English

MFC的CString的奇怪行为

[英]strange behavior with MFC's CString

I am getting the strangest memory behavior using an MFC CString: 我使用MFC CString获得最奇怪的内存行为:

line 138: PCD_Dir = m_pRunPCD->m_PCD_Dir; 第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. 结果是strSrc和m_pRunPCD-> m_PCD_Dir的地址偏移了1,这就是缓冲区指针无效的原因。 turns out I had incorrectly enabled packing in a header file to resolve another issue which led to this issue. 事实证明我错误地启用了在头文件中打包以解决导致此问题的另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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