for (int i = 0 ; i < m_List.GetCount();)
{
       POSTION pos  = m_List.Findindex(i);
       ......
       Cbutton * lpBtn = m_List.GetAt(pos);
       if(lpBtn->Stata == 1)
       {
          SendMessage(.....,(lParam)lpbtn);
   //在Sendmessge(里面没对mlist做任何操作,去掉sendmssage则不会出现,这种情况)这个之后 pos的 值   就发生了 变化,导致 remove的时候crash,
            还发现  整个 M_list 的Note 节点的内存全部发生了变化,但整个M_list数据时完好的,
            似乎只是整体搬迁了个位置?
            有人能解释下为什么不啊,实在想不通。
           1.m_list节点的内存 会整体搬迁吗?
           2.想遍历 m_list 删除 某几个元素,怎样写安全啊?

           m_List.Remove(pos);
          lpBtn->DestoryWindow();       
        }else
        {
            i++;
         }}

解决方案 »

  1.   

    CList<int, int&> test;
    for(int i = 0; i != 10; ++i)
    {
    test.AddTail(i);
    } CString strTemp(_T(""));
    CString strText(_T(""));
    POSITION pos = test.GetHeadPosition();
    while(pos)
    {
    strTemp.Format(_T("%d, "), test.GetAt(pos));
    strText += strTemp;
    test.GetNext(pos);
    } AfxMessageBox(strText);

    pos = test.GetTailPosition();
    while(pos)
    {
    int nValue = test.GetAt(pos);
    switch(nValue)
    {
    case 3:
    case 4:
    case 5:
    case 6:
    test.RemoveAt(pos);
    break; default:
    break;
    }
    test.GetPrev(pos);
    } pos = test.GetHeadPosition();
    strText.Empty();
    while(pos)
    {
    strTemp.Format(_T("%d, "), test.GetAt(pos));
    strText += strTemp;
    test.GetNext(pos);
    }

    AfxMessageBox(strText);