0

我想我刚刚发现了问题(也许没有?),但我想了解原因。以下是代码快照。我试图让它尽可能简单;

class FOO
{
public:
    int m_iNum; //sort according to this.
    std::vector<unsigned char>::iterator m_itContents; 
            //filling this contributes to issue with sorting in debug only? 
            //m_Contents does not come into other  sections below at all.
    unsigned char *m_Contents; //using this instead, 
                                       //no asserts or crash in debug.
};

class MyFile
{
public:
    std::vector<FOO> vFoo1; //this gets filled with file data.
};

std::vector<FOO> vFooVirtualList; //class member

int iColumnSel = -1;
int iSortResult1 = 1;
int iSortResult2 = -1;
int iVirtualListCount = 0;


void FillList()
{
    std::vector<Foo>::iterator itFoo;
    itFoo = MyFile->Foo1.begin() ;
    iVirtualListCount = 0;

    vFooVirtualList.clear();
    std::vector<Foo> vTemp;
    vTemp.swap( vFooVirtualList );
    vFooVirtualList.reserve(FileCount); // int filecount 

    for(int i=0;i< FileCount ;i++ ,itFoo++) 
            {
            vFooVirtualList.push_back(*itFoo);
            iVirtualListCount++;
    }
    // Tell the list box to update itself.
    m_List.SetItemCountEx(iVirtualListCount); 
            //m_List is CListCtrl, used as virtual list.
    m_List.Invalidate();
}

void OnLvnGetdispinfoMainframelist(NMHDR *pNMHDR, LRESULT *pResult)
{
    NMLVDISPINFO *pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);
    LV_ITEM* pItem= &(pDispInfo)->item;
    std::vector<FOO>::iterator itFooList;
    itFooList = vFooVirtualList.begin() + pItem->iItem;
    CString str;

    if(pItem->mask & LVIF_TEXT)
    {
        // then display the appropriate column
        switch(pItem->iSubItem)
        {
        case 0:
            str.Format(L"%d",itFooList->m_iNum);  
            lstrcpy(pItem->pszText, str);
            break;

        default:
            break;
        }
    }
    *pResult = 1;
}

//sort by column
void OnLvnColumnclickMainframelist(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);    
    for(iColumnSel = 0; iColumnSel < 1; iColumnSel++)
    {
        if(iColumnSel == pNMLV->iSubItem)
            SortByCol(iColumnSel);
    }
    // Redraw the list.
    m_List.Invalidate();        
    *pResult = 0;
}

//sorting done based only on Num.
int __cdecl CompareByFileNum(const void *elem1, const void *elem2)
{
    int result = 0;
    FOO *Item1 = (FOO*)elem1;
    FOO *Item2 = (FOO*)elem2;

    switch(iColumnSel)
    {
        case 0:
            if(Item1->m_iNum <  Item2->m_iNum)
                result = iSortResult1;
            if(Item1->m_iNum > Item2->im_Num) 
                result = iSortResult2;
            return result;
            break;
        default:
            break;
    }
    return 1;
}

void SortByCol(const int ColIndex)
{
    //fill list. sort. then load another file = crash. Only in debug mode?
    //if list is in proper order, no problem on new file load. otherwise crash.
    qsort( static_cast<void*>(&vFOOVirtualList[0]), iVirtualListCount, sizeof(FOO), CompareByFileNum);
    iSortResult1 = (iSortResult1*-1);
    iSortResult2 = (iSortResult2*-1);
}

想知道迭代器有什么特别需要做的吗? FillList()这里工作正常,除了在第二次调用(即新文件)时,如果虚拟列表的顺序与第一次不同,新文件崩溃,但仅在调试模式下,并且仅当我使用迭代器而不是指针时。在任何一种情况下,我都会得到“ iterator list corrupted or Unhandled exception at 0x0042c509”(略有变化)和引用 xutility,分别是第 181 行或第 177 行。

4

0 回答 0