SetClipboardData() 期间堆损坏
我不确定以下代码中出现此类错误(堆损坏)的根本原因是什么。当我单步执行程序时,TCHAR 值被正确分配并复制到剪贴板数据。但是,当继续执行 SetClipboardData(...) 时,它会崩溃。
有哪位高手可以帮忙找出错误吗?
提前致谢。
错误对话框:
04A781C0处的堆块修改于 04A78282 过去要求的 ba 尺寸 Windows 已触发断点 V4.exe。
这可能是由于 堆,这表明 V4.exe 中存在错误 或其已加载的任何 DLL。
这也可能是由于用户 当 V4.exe 获得焦点时按 F12。
输出窗口可能有更多 诊断信息。该计划 “[10840] V4.exe:本机”已退出 代码为 0 (0x0)。
代码:
int nCount = m_ListBox.GetCount();
CString szTemp, szText;
for(int i=0; i<nCount; i++)
{
m_ListBox.GetText(i, szTemp);
szText = szText + _T("\n") + szTemp;
}
if(OpenClipboard())
{
EmptyClipboard();
HGLOBAL hClipboardData;
size_t size = (szText.GetLength()+1) * sizeof(TCHAR);
hClipboardData = GlobalAlloc(NULL, size);
TCHAR* pchData = (TCHAR*)GlobalLock(hClipboardData);
_tcscpy_s(pchData, size, LPCTSTR(szText));
#ifdef _UNICODE
SetClipboardData(CF_UNICODETEXT, hClipboardData); //--> crash here
#else
SetClipboardData(CF_TEXT, hClipboardData);
#endif
GlobalUnlock(hClipboardData);
CloseClipboard();
}
列表框数据:
John Smith 1978
Angelina 1975
Brad Pitt 1950
I'm not sure what is the root cause for getting such error(Heap Corruption) from the below code. When i step through the program, the TCHAR value is properly allocated and copied to the clipboard data. However, it crash when it proceed to SetClipboardData(...).
Can any guru help to spot the error?
Thanks in advance.
Error Dialog:
Heap block at 04A781C0 modified at
04A78282 past requested size of ba
Windows has triggered a breakpoint in
V4.exe.This may be due to a corruption of the
heap, which indicates a bug in V4.exe
or any of the DLLs it has loaded.This may also be due to the user
pressing F12 while V4.exe has focus.The output window may have more
diagnostic information. The program
'[10840] V4.exe: Native' has exited
with code 0 (0x0).
Code:
int nCount = m_ListBox.GetCount();
CString szTemp, szText;
for(int i=0; i<nCount; i++)
{
m_ListBox.GetText(i, szTemp);
szText = szText + _T("\n") + szTemp;
}
if(OpenClipboard())
{
EmptyClipboard();
HGLOBAL hClipboardData;
size_t size = (szText.GetLength()+1) * sizeof(TCHAR);
hClipboardData = GlobalAlloc(NULL, size);
TCHAR* pchData = (TCHAR*)GlobalLock(hClipboardData);
_tcscpy_s(pchData, size, LPCTSTR(szText));
#ifdef _UNICODE
SetClipboardData(CF_UNICODETEXT, hClipboardData); //--> crash here
#else
SetClipboardData(CF_TEXT, hClipboardData);
#endif
GlobalUnlock(hClipboardData);
CloseClipboard();
}
List Box data:
John Smith 1978
Angelina 1975
Brad Pitt 1950
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 Unicode wcscpy_s 函数,大小参数是以字为单位的大小,并且您传递以字节为单位的大小。这可能会导致内存损坏,因为 wcscpy_s 在复制之前用 0xFD 填充所有缓冲区,以便捕获此类错误。
(感谢 Sharptooth 提供准确信息)。
For Unicode wcscpy_s function, size parameter is size in words, and you pass size in bytes. This may cause memory corruption, because wcscpy_s fills all the buffer with 0xFD prior to copying, in order to catch such errors.
(thanks to sharptooth for exact information).
以下是 MSDN 的引用
设置剪贴板数据
:由于您将 NULL 传递给
OpenClipboard
,SetClipboardData
失败。Following is the quote from the MSDN for
SetClipboardData
:Since you are passing NULL to
OpenClipboard
,SetClipboardData
is failing.在调用
SetClipboardData(CF_UNICODETEXT, hClipboardData);
之前调用GlobalUnlock(hClipboardData);
Call
GlobalUnlock(hClipboardData);
before the call toSetClipboardData(CF_UNICODETEXT, hClipboardData);