如何追踪变量腐败
我有一个从 CStdioFile 派生的 MFC 类,声明如下:
// Datafile.h
class CDataFile : public CStdioFile
{
public:
CDataFile(void);
~CDataFile(void);
int OpenFile(LPCWSTR FileName);
}
在调用我的 OpenFile 函数后,FileName 变量被损坏。
int CDataFile::OpenFile(LPCWSTR FileName)
{
m_OpenFlags = CFile::modeNoTruncate | CFile::modeReadWrite;
// Before open. FileName = "c:\afile.txt"
if (!Open(FileName, m_OpenFlags, NULL))
{
return GetLastError();
}
//After open. FileName = ""ﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮވĚᗸ÷ᘸ÷㼠碞"
// other stuff
}
}
但是,如果我将文件名更改为
WCHAR FileName[] = _T("c:\\afile.txt");
打开文件之前,则变量文件名保持不变。我之前在 MFC/Winapi 中见过这种行为,并且总是通过使用字符数组而不是 LPCWSTR 或 CString 来解决它。为什么会发生这种情况?以及使用 VS 调试器追踪此类问题的最佳方法是什么。损坏似乎发生在 MFC 文件 Filecore.cpp 中
if (!CFile::Open(lpszFileName, (nOpenFlags & ~typeText), pException))
return FALSE;
I have a MFC class derived from CStdioFile declared as follows
// Datafile.h
class CDataFile : public CStdioFile
{
public:
CDataFile(void);
~CDataFile(void);
int OpenFile(LPCWSTR FileName);
}
After my OpenFile function is called the FileName variable is being corrupted.
int CDataFile::OpenFile(LPCWSTR FileName)
{
m_OpenFlags = CFile::modeNoTruncate | CFile::modeReadWrite;
// Before open. FileName = "c:\afile.txt"
if (!Open(FileName, m_OpenFlags, NULL))
{
return GetLastError();
}
//After open. FileName = ""ﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮވĚᗸ÷ᘸ÷㼠碞"
// other stuff
}
}
But if I change the FileName to
WCHAR FileName[] = _T("c:\\afile.txt");
Before opening the File the variable Filename remains untouched. I have seen this behaviour before with the MFC/Winapi and always worked around it be by using character arrays instead of LPCWSTR or CString. Why does this happen? and what is the best way to track down problems such as this with the VS Debugger. The corruption appears to happen here in the MFC file Filecore.cpp
if (!CFile::Open(lpszFileName, (nOpenFlags & ~typeText), pException))
return FALSE;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下使用数据断点(也可以称为硬件断点。)您可以在内存被修改时中断。
Take a look at using a data breakpoint (also known as a hardware breakpoint.) You can break when the memory is modified.