刚才发了另一个帖子问,但是后边我老贴错代码, 从新发一贴。void CExp65Dlg::OnButton1() 
{

     CString * str; 
     str=new CString(); 
     str->Format("110"); 
     m_list.AddString("first");
     m_list.SetItemData(1,(DWORD)str); //m_list为ListCtr }void CExp65Dlg::OnButton2() 
{    CString *str;
    str=(CString *)m_list.GetItemData(1); 
    AfxMessageBox(*str);            <------------------------------运行时到这步就报XXX内存不能为“read”的
                                                                   错误。我很迷
}
先谢谢刚才在那个帖子中帮我的 鹦鹉 和另两位老师。   

解决方案 »

  1.   

    很简单 . 你的str变量时临时的, 当函数体运行完后, 内存被销毁, 指针无效 . 
    解决方法, 声明一个全局或者成员变量 .
      

  2.   

    GetItemData 不是返回字符串的地址  你*str当然错啦 它返回一个32位的值 你的内容是个字符串,他返回前4个字节的int值  你到这个int表示的地址去取内容当然就错啦  要得到字符串用GetText  控件怎么用看MSDN去
      

  3.   

    str=new CString();  还内存泄露了
      

  4.   

    你是没看完就回答了。没看到是new的么
      

  5.   

    你OnButton1的时,列表中有没有记录?
       m_list.AddString("first"); 
        m_list.SetItemData(1,(DWORD)str); //m_list为ListCtr 
    如果这个first是第一条,那么应该是m_list.SetItemData(0,(DWORD)str)才对。
      

  6.   

    m_list.GetItemText(1,0);这个才是取内容的
      

  7.   

    貌似试了一下,set0,get0也是报错。
      

  8.   

    你又是哪位?????????????????
    难道你自己不能进行很简单的调试,看一下GetItemData返回的是什么吗?
    最绝对的,
    int nIdx = m_list.AddString("first");
    m_list.SetItemData(nIdx,(DWORD)str);
      

  9.   

    问题解决了,谢谢所有帮助我的朋友。 问题原因是因为 我加入数据时list里边是空的,位置应该是0而不是1, 再次感谢。分不多。以后我会努力挣分回报。
      

  10.   

    只addstring了,没有insertitem
    void CTttDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
    CString *str;
        str=new CString();
        str->Format("110");
        //m_list.AddString("first"); m_list.InsertItem(0,"110");
        m_list.SetItemData(0,(DWORD)str); //m_list为ListCtr 
    CString *i = (CString *)m_list.GetItemData(0);
        i = 0;
    }void CTttDlg::OnButton2() 
    {
    // TODO: Add your control notification handler code here
    CString *str1;
        str1 = (CString *)m_list.GetItemData(0);
        AfxMessageBox(*str1);     
    }
    这是好的
      

  11.   

    调试了get直接报错后来发现没有addstring,我还是习惯insertitem....
      

  12.   

    对的,要么你在m_list.AddString("first"); 以后再加一个,如m_list.AddString("first2"); 
    在你的void CExp65Dlg::OnButton2() 函数最后,加上delete str;