虚拟列表控制问题

发布于 2024-09-10 21:32:36 字数 940 浏览 0 评论 0原文

我正在使用虚拟照明控件,并从地图获取数据。我的问题是,当我运行代码时,它显示列表正常,但是当鼠标光标移动到列表控件上或当我尝试向下滚动时,它会给出调试断言失败,指出映射/设置迭代器不可取消引用。我的 GetDispInfo() 方法如下:

void CListCtrlTestDlg::GetDispInfo(NMHDR* pNMHDR, LRESULT* pResult) 
{
   LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
   LV_ITEM* pItem = &(pDispInfo)->item;

   map<int, Error_Struct>::iterator it  = Error_Map.find((pItem->iItem) + 1);
   int iErrCode = (*it).second.i_ErrorCode;
   CString cError = (*it).second.c_Error;


    switch(pItem->iSubItem)
    {
    case 0:
        sprintf_s(pItem->pszText, 10, "[ %d ]", iErrCode);
        break;
    case 1:
        sprintf_s(pItem->pszText, 100, "%s", cError);
        break;
    }


*pResult = 0;

}

另外,如果当鼠标指针位于列表控件的顶部时,程序再次崩溃,并显示 output.c 文件中下面显示的行中的访问冲突:

#else  /* _UNICODE */
   if (_putc_nolock(ch, f) == EOF)

有人有相同的经历吗?我在这里做错了什么以及如何解决这个问题?

谢谢你!

I'm using a virtual lit control and I get the data from a map. My problem is when I run the code, it displays the list ok, but when the mouse cursor moves on to the list control or when I try to scroll down, it gives a Debug Assertion failure saying map/set iterator is not dereferencable. My GetDispInfo() method is as follows:

void CListCtrlTestDlg::GetDispInfo(NMHDR* pNMHDR, LRESULT* pResult) 
{
   LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
   LV_ITEM* pItem = &(pDispInfo)->item;

   map<int, Error_Struct>::iterator it  = Error_Map.find((pItem->iItem) + 1);
   int iErrCode = (*it).second.i_ErrorCode;
   CString cError = (*it).second.c_Error;


    switch(pItem->iSubItem)
    {
    case 0:
        sprintf_s(pItem->pszText, 10, "[ %d ]", iErrCode);
        break;
    case 1:
        sprintf_s(pItem->pszText, 100, "%s", cError);
        break;
    }


*pResult = 0;

}

Also if when the mouse pointer is on top of the list control, again the program crashes saying access violation from the line showed below in output.c file:

#else  /* _UNICODE */
   if (_putc_nolock(ch, f) == EOF)

Has anyone got the same experience? What am I doing wrong here and how can I fix this problem?

Thank You!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海夕 2024-09-17 21:32:36

对于初学者来说,您如何知道传递给 sprintf_s 的神奇常量 10 和 100 实际上是 pItem->pszText 中正确的空间量?您应该使用 pItem->cchText。

其次,您可能应该检查从 std::map::find 返回的迭代器是否有效。

For starters, how do you know that your magic constants 10 and 100, that you're passing to sprintf_s are actually the correct amount of space in pItem->pszText? You should use pItem->cchText.

Second, you should probably check that the iterator returned from std::map::find is valid.

司马昭之心 2024-09-17 21:32:36

实际问题是我试图将数据复制到 LV_ITEM 结构的非缓冲区成员。我试图将数据复制到一个纯粹的指针,这就是问题所在。我真正应该做的是为该指针分配一个值而不复制数据。

p = o_RBTree.FindByPosition((pItem->iItem) + 1);

char zKey[10];
zKey[0] = '\0';
sprintf_s(zKey, 10, "%d", p.first);

char zVal[100];
zVal[0] = '\0';
sprintf_s(zVal, 100, "%d", p.second);

if (pItem->mask && LVIF_TEXT) 
{
    switch(pItem->iSubItem)
    {
    case 0:
        pItem->pszText = zKey;
        break;
    case 1:
        pItem->pszText = zVal;
        break;
    }
}

这非常有效!感谢您的帮助。

The actual problem was that I was trying copy data to a non-buffer member of LV_ITEM structure. I was trying to copy data to a mere pointer, that was the problem. What I should have really done was to assign a value to that pointer without copying data.

p = o_RBTree.FindByPosition((pItem->iItem) + 1);

char zKey[10];
zKey[0] = '\0';
sprintf_s(zKey, 10, "%d", p.first);

char zVal[100];
zVal[0] = '\0';
sprintf_s(zVal, 100, "%d", p.second);

if (pItem->mask && LVIF_TEXT) 
{
    switch(pItem->iSubItem)
    {
    case 0:
        pItem->pszText = zKey;
        break;
    case 1:
        pItem->pszText = zVal;
        break;
    }
}

This works perfectly! Thanks for the help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文