char *temppath= new char[40];
ZeroMemory(temppath,40);
temppath = m_filepath.GetBuffer(0);
while (strlen(temppath) < 40)
{
strcat(temppath," ");
}
file.Write(temppath,40);
delete temppath;请问为什么运行的时候  delete temppath;的时候  出现
File:dbgheap.c ,Line:1011 ,Expression:_Crtls ValidHeapPointer的错误.
delete []temppath;也一样..

解决方案 »

  1.   

    你是说数组越界了?
    temppath 肯定没有呀.
      

  2.   

    把delete temppath; 这句注释点就不会报错了.
    但是退出的时候就会报内存泄露..
      

  3.   

    明白了  
    temppath = m_filepath.GetBuffer(0); 这里.........    
      

  4.   



    temppath = m_filepath.GetBuffer(0);  

    替换成
    if(m_filepath.GetLength()<40)
    {
       strcpy(temppath,m_filepath.GetBuffer(0));
    }
      

  5.   

    char *temppath;
    temppath = m_filepath.GetBuffer(40);
    while (strlen(temppath) < 40)
    {
    strcat(temppath," ");
    }
    file.Write(temppath,40);
    m_filepath.ReleaseBuffer();
      

  6.   

    temppath指向的是CString内部分配的空间,你删除它当然出错了。
    改成这样:
    char *temppath= new char[40];
    ZeroMemory(temppath,40);
    //temppath = m_filepath.GetBuffer(0);
    strcpy(temppath,m_filepath.GetBuffer(0));
    while (strlen(temppath) < 40)
    {
    strcat(temppath," ");
    }
    file.Write(temppath,40);
    delete[] temppath;
      

  7.   

    m_filepath.GetBuffer(0);
    ---------------------------
    你修改了temppath的地址了
      

  8.   

    m_filepath.GetBuffer(0);
    为什么一定要这样用呢?
    这样多好
    m_filepath.GetBuffer(1024/*或者更大*/);
      

  9.   

    char *temppath= new char[40];
    ZeroMemory(temppath,40);
    temppath = m_filepath.GetBuffer(0);
    while (strlen(temppath) < 40)
    {
    strcat(temppath," ");
    }
    file.Write(temppath,40);
    delete temppath;
    还有,你既然这样:(1)
    char *temppath= new char[40];
    那为什么还要这样:(2)
    temppath = m_filepath.GetBuffer(0);
    会内存泄漏的。
    既然已经那样(2)了,这样做也不对
    delete temppath;还有。
    GetBuffer之后一定要ReleaseBuffer()
    m_filepath.ReleaseBuffer()
      

  10.   

    char *temppath= new char[40];
    ZeroMemory(temppath,40);
    temppath = m_filepath.GetBuffer(0);
    这样写有错误,新申请的40个字节的空间被丢弃导致内存泄露,应该写作:
    memcpy(temppath,m_filepath.GetBuffer(0),m_filepath.GetLength());
    使用完毕再:delete[] temppath;