views:

56

answers:

2

Hi,

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!

+1  A: 

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.

Roger Lipscombe
A: 

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.