请问个问题

crosoli 2010-07-23 01:41:41


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;也一样..
...全文
125 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Liberty-Bird 2010-07-24
  • 打赏
  • 举报
回复
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;
Sou2012 2010-07-23
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 weizehua 的回复:]

C/C++ code
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……
[/Quote]

讲得很清楚了。
向立天 2010-07-23
  • 打赏
  • 举报
回复
你这个用的太混乱了
Happy0403 2010-07-23
  • 打赏
  • 举报
回复
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()

Happy0403 2010-07-23
  • 打赏
  • 举报
回复
m_filepath.GetBuffer(0);

为什么一定要这样用呢?
这样多好
m_filepath.GetBuffer(1024/*或者更大*/);
Eleven 2010-07-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 crosoli 的回复:]
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;

……
[/Quote]
m_filepath.GetBuffer(0);
---------------------------
你修改了temppath的地址了
wltg2001 2010-07-23
  • 打赏
  • 举报
回复
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;

crosoli 2010-07-23
  • 打赏
  • 举报
回复
char *temppath;
temppath = m_filepath.GetBuffer(40);
while (strlen(temppath) < 40)
{
strcat(temppath," ");
}
file.Write(temppath,40);
m_filepath.ReleaseBuffer();
昨夜无风 2010-07-23
  • 打赏
  • 举报
回复


temppath = m_filepath.GetBuffer(0);

替换成
if(m_filepath.GetLength()<40)
{
strcpy(temppath,m_filepath.GetBuffer(0));
}
crosoli 2010-07-23
  • 打赏
  • 举报
回复
明白了
temppath = m_filepath.GetBuffer(0);

这里.........
crosoli 2010-07-23
  • 打赏
  • 举报
回复
把delete temppath; 这句注释点就不会报错了.
但是退出的时候就会报内存泄露..
crosoli 2010-07-23
  • 打赏
  • 举报
回复
你是说数组越界了?
temppath 肯定没有呀.
cdsnpeter 2010-07-23
  • 打赏
  • 举报
回复
这种错误一般是越界了。。。

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧