首先在一个循环里每次都new一个节点,然后添加到一个链表中。
然后初始化一个CStdioFile File。发现CStdioFile类中有个变量m_strFileName指向的内存地址和链表中某个节点地址是一样的,如果执行File.Open命令,则出错。
我想知道,为什么这种动态分配的内存会出现冲突? 
链表的节点个数大概165以内是不出错的,166时出错,且第166个节点的地址和初始化的CStdioFile文件地址一致。

解决方案 »

  1.   

    贴代码我遇到过直接初始化变量,造成变量的地址和函数内的临时变量地址重复。
    就是 int a  =  fun();
    改成 int a ; a = fun();解决,当时这个问题找了好久,最终也不明白为什么。
    楼主看看是否有类似问题。
      

  2.   

    以下代码是从一个opc服务器中提取出来的。
    //这是初始化时读一次文件,是不出错的。
    CStdioFile File;
    CString StrFileName=_T("F:\\db\\realdata.txt");
    //realdata.txt中每一行存着点名,点类型和点值。以分号间隔
    if(File.Open(StrFileName,CFile::modeRead|CFile::typeText))
    {
    CString Line;
    while (File.ReadString(Line))
    {
    CTokenizer tok(Line);
    int size = tok.GetSize();
    RecordNode* pRecnode=new RecordNode;
    int count = 0;
    CString str;
    tok.GetAt(count++,pRecnode->Record_Name );
    tok.GetAt(count++, str);
    if ((str == _T("0"))||(str == _T("1")))
    {
    pRecnode->Record_Values.vt = VT_BOOL;
    tok.GetAt(count++, str);
    pRecnode->Record_Values.boolVal = _ttoi(str);
    }
    else
    {
    pRecnode->Record_Values.vt = VT_R4;
    tok.GetAt(count++, str);                
    #ifdef _UNICODE

    pRecnode->Record_Values.fltVal = wtof(str);
    #else
                   pRecnode->Record_Values.fltVal = atof(str);              
    #endif 
    }  g_RecordList.AddTail(pRecnode);
    }
    File.Close();
    return(TRUE);
    }
    return(FALSE);
    //添加点之后循环读取文件内容,代码如下:
    CStdioFile File;
    //这一句File的定义,从调试栏中看到File文件下有个变量叫m_strFileName的地址和初始化时添加第166个点地址一致。然后就出错了。当realdata.txt文件中点数不到166时,运行正常。这个数值也不是确定就是166.有时可能是170.我换一个内存大的电脑仍然出错。如有的朋友说把代码摘出来,我试过了,摘出来的确没有错误。所以我怀疑是别的地方代码影响到了这里。但不知原因。
    CString StrFileName=_T("F:\\db\\realdata.txt");
    if(File.Open(StrFileName,CFile::modeRead|CFile::typeText))
    {
    CString Line;
    VARIANT vValues;
    CString strName="";
    CString strType="";
    CString strValue="";
    while (File.ReadString(Line))
    {
    CTokenizer tok(Line);
    int size = tok.GetSize();
    int count = 0;

    tok.GetAt(count++, strName);
    tok.GetAt(count++, strType);

    #ifdef _UNICODE 
    if(!wcscmp((LPCTSTR)(LPCTSTR)strName,m_szItemID))
    #else
    USES_CONVERSION;
    if (A2W(strName)==m_szItemID) 
    #endif
    {
    if ((strType == _T("0"))||(strType == _T("1")))
    {
    vValues.vt = VT_BOOL;

    tok.GetAt(count++, strType);
    vValues.boolVal = _ttoi(strType);
    }
    else
    {
    vValues.vt = VT_R4;

    tok.GetAt(count++, strType);                
    #ifdef _UNICODE
    vValues.fltVal = wtof(strType);
    #else
    vValues.fltVal = atof(strType);              
    #endif 
    }
    SetValue(&vValues);
    break; }
    }
    File.Close();
    }
    还有,假设我不定义File文件,我初始化一个CStringArray,竟然地址也冲突,和另外一个数据点冲突。
      

  3.   

    应该不会的吧!
    new是在堆里分配的局部变量是在栈里分配的,怎么会冲突呢?
      

  4.   

    我怀疑是别的程序有错误,但是没有报错。影响到了CStdioFile的初始化。
    但是查不到出错的地方。