高手!!怎么解决这个芮存泄漏??

Niou_Shi 2002-03-01 10:01:12
long g_ParsingParameters(PARAINFO& para, LPCTSTR prg_info, long svr_count, LPCTSTR svr_info, long url_count, LPCTSTR url_info, long ftp_count, LPCTSTR ftp_info)
//½âÎö²ÎÊýÐÅÏ¢
{
CString str, str1, str2;
CString tag;
LPTSTR pstr,pstr1,pstr2,ptag,p;
pstr=str.GetBuffer(1024);
pstr1=str1.GetBuffer(1024);
pstr2=str2.GetBuffer(1024);
ptag=tag.GetBuffer(1024);
int nStart, nEnd;
int i, nPos, nPos0, num;

para.bIsSchedule = FALSE;
//get program information
// str=prg_info;
strcpy(pstr,prg_info);

str.ReleaseBuffer();//ÓÃstrµÄ³ÉÔ±º¯ÊýǰҪÏÈReleaseBuffer
// tag = _T("NAME=");
strcpy(ptag,"NAME=");
tag.ReleaseBuffer();
nStart = str.Find(tag);
if (nStart == -1) return 1;
// tag = _T(";");
ptag=tag.GetBuffer(1024);
strcpy(ptag,";");
tag.ReleaseBuffer();
nEnd = str.Find(tag, nStart);
if (nEnd == -1) return 1;

strcpy(pstr2,str.Mid(nStart+5, nEnd-nStart-5));//1,不会泄漏
str2.ReleaseBuffer();//
str2.TrimLeft();
str2.TrimRight();
para.prg_info.name = str2;



//tag = _T("ID=");
ptag=tag.GetBuffer(1024);
strcpy(ptag,"ID=");
tag.ReleaseBuffer();

nStart = str.Find(tag);
if (nStart == -1) return 1;
#ifdef _DEBUG
CMemoryState oldMemState, newMemState, diffMemState;
oldMemState.Checkpoint();
#endif
// tag = _T(";");
ptag=tag.GetBuffer(1024);
strcpy(ptag,";");
tag.ReleaseBuffer();

nEnd = str.Find(tag, nStart);
if (nEnd == -1) return 1;


p=str2.GetBuffer(1024);
strcpy(p,str.Mid(nStart+3, nEnd-nStart-3));//2,这里泄漏!!why
str2.ReleaseBuffer();
str2.TrimLeft();
str2.TrimRight();
#ifdef _DEBUG
newMemState.Checkpoint();
if(diffMemState.Difference(oldMemState,newMemState))
{
TRACE("mem leak in RWF");
diffMemState.DumpStatistics();
}

#endif
。。。。
}

泄漏信息为:
0 bytes in 0 Free Blocks.
1037 bytes in 1 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 0 bytes.
Total allocations: 1065 bytes.
...全文
72 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhang1000 2002-03-01
  • 打赏
  • 举报
回复
好像是长度的问题,看看MSDN中的说明:
Note that if you keep track of the string length yourself, you should not append the terminating null character. You must, however, specify the final string length when you release the buffer with ReleaseBuffer. If you do append a terminating null character, you should pass –1 for the length to ReleaseBuffer and ReleaseBuffer will perform a strlen on the buffer to determine its length
不好意思,我没试过.
cai_c_cai 2002-03-01
  • 打赏
  • 举报
回复
你的代码根本就没有内存leak;只是检测方法不对。

你这样检测肯定会出现leak。CString都是动态分配内存的,他在赋值时动态分配内存或者调用GetBuffer(),他在析构时才会释放内存。CString的
ReleaseBuffer()并不是释放内存,而是使GetBuffer()获得的内存有效,即让其他CString函数的操作有效。

比较下面两段代码就会明白。
出现leak的代码段
{
CString str;
#ifdef _DEBUG
CMemoryState oldMemState, newMemState, diffMemState;
oldMemState.Checkpoint();
#endif
str="aa";//
#ifdef _DEBUG
newMemState.Checkpoint();
if(diffMemState.Difference(oldMemState,newMemState))
{
TRACE("mem leak in RWF");
diffMemState.DumpStatistics();
}
}
不出现leak的代码段:
{
#ifdef _DEBUG
CMemoryState oldMemState, newMemState, diffMemState;
oldMemState.Checkpoint();
#endif
{
CString str;
str="aa";
}
#ifdef _DEBUG
newMemState.Checkpoint();
if(diffMemState.Difference(oldMemState,newMemState))
{
TRACE("mem leak in RWF");
diffMemState.DumpStatistics();
}
}
这两段代码的差别就是一个CString没有析构,一个CString析构了。









16,551

社区成员

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

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

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