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;也一样..
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;也一样..
temppath 肯定没有呀.
但是退出的时候就会报内存泄露..
temppath = m_filepath.GetBuffer(0); 这里.........
temppath = m_filepath.GetBuffer(0);
替换成
if(m_filepath.GetLength()<40)
{
strcpy(temppath,m_filepath.GetBuffer(0));
}
temppath = m_filepath.GetBuffer(40);
while (strlen(temppath) < 40)
{
strcat(temppath," ");
}
file.Write(temppath,40);
m_filepath.ReleaseBuffer();
改成这样:
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;
---------------------------
你修改了temppath的地址了
为什么一定要这样用呢?
这样多好
m_filepath.GetBuffer(1024/*或者更大*/);
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()
ZeroMemory(temppath,40);
temppath = m_filepath.GetBuffer(0);
这样写有错误,新申请的40个字节的空间被丢弃导致内存泄露,应该写作:
memcpy(temppath,m_filepath.GetBuffer(0),m_filepath.GetLength());
使用完毕再:delete[] temppath;