lstrcpy() 导致 Visual C++ 中出现异常代码
我使用 MFC 虚拟列表控件来增强性能,并处理 GetDispInfo(NMHDR* pNMHDR, LRESULT* pResult) 来填充 ListCtrl。该方法中相关代码如下:
if (pItem->mask && LVIF_TEXT) {
switch(pItem->iSubItem)
{
case 0:
lstrcpy(pItem->pszText, rLabel.m_strText);
break;
case 1:
sprintf(pItem->pszText, "%d", p.o_Value);
break;
default:
ASSERT(0);
break;
}
}
在这里,当我使用 lstrcpy() 时,当我向下/向上滚动时,我收到很多异常,显示First-chance exception at 0x7c80c741 in test_list_control.exe: 0xC0000005: Access Violationwriting location 0xb70bf2ac。< /strong> 这些消息出现在调试输出中。但程序不会崩溃。谁能解释一下这里出了什么问题以及我应该如何克服这个问题?
rLabel 是我之前声明的一个 CLabelItem。
谢谢你!
I used a MFC virtual list control to enhance the performance and I handle GetDispInfo(NMHDR* pNMHDR, LRESULT* pResult) to populate the ListCtrl. The relevant code in that method is as follows:
if (pItem->mask && LVIF_TEXT)
{
switch(pItem->iSubItem)
{
case 0:
lstrcpy(pItem->pszText, rLabel.m_strText);
break;
case 1:
sprintf(pItem->pszText, "%d", p.o_Value);
break;
default:
ASSERT(0);
break;
}
}
Here, when I use lstrcpy(),when I'm srolling down/up, I get a whole lot of exceptions saying First-chance exception at 0x7c80c741 in test_list_control.exe: 0xC0000005: Access violation writing location 0xb70bf2ac. These messages appear in the debug output. But the program doesn't crash. Can anyone please explain what the matter here and how should I overcome that??
rLabel is a CLabelItem which I have declared earlier.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您看到的只是第一次机会异常,请停止担心。例如,请参阅链接 但您可以在各处找到类似的页面(主要是 5-10 年前)。这意味着抛出了一些代码,并且捕获并处理了异常。我有时会在 MFC 应用程序中看到这种情况。正如博客文章所说
我会等到你看到实际的错误后再开始处理这个问题。
If all you see is the first chance exception thing, stop worrying. See for example Link but you can find similar pages all over the place (mostly from 5-10 years ago.) It means some code threw and the exception was caught and dealt with. I see this in MFC apps some times. As the blog entry says
I would wait until you see actual errors before getting worked up about this one.
我认为您应该检查 pItem->pszText 指向的缓冲区是否足够大以容纳 rLabel.m_strText。或者,如果 rLabel.m_strText 是正确的空终止字符串。对我来说,这看起来就像写入未初始化的内存。使用调试器来检查这一点。
I think you should check if buffer that is pointed by pItem->pszText is large enough to hold rLabel.m_strText. Or if rLabel.m_strText is correct null terminated string. For me this looks like writing uninitialized memory. Use the debugger to check this.