内存泄露问题~~~

snowbirdfly 2005-12-14 04:11:26
//附件处理。主要是用于传送邮件的附件~~
但是下面
if(!m_file.IsEmpty())
{
char filename[256];
char* szTemp;
wsprintf(filename,"%s",m_file);

FILE* hFile=fopen(filename,"a+");




//获得文件大小
DWORD FileSize=0;
char temp[2];
while(fread(temp,1,1,hFile))
{
FileSize++;
}
fseek(hFile,0,0);

//读取文件内容
szTemp=(char*)malloc(FileSize);
fread(szTemp,FileSize,1,hFile);

char* szBuffer=(char*)malloc(FileSize);

mySmtp.base64(szTemp,szBuffer);
free(szTemp);

char* temp1=(char*)malloc(FileSize+200);
strcpy(temp1,"\r\nContent-Type:application/octet-stream;Name=%s\r\n");
strcat(temp1,"Content-Disposition:attachment;FileName=%s\r\n");
strcat(temp1,"Content-Transfer-Encoding:Base64\r\n\r\n");
strcat(temp1,"%s\r\n\r\n");

char* body=(char*)malloc(FileSize+400);

char temp2[256];
strcpy(temp2,strtok(filename,"\\"));

strcpy(temp2,strtok(NULL,"\\"));
wsprintf(body,temp1,temp2,temp2,szBuffer);
strcat(sendinfo.msg,body);
free(temp1);
free(body);
}
这个不知道是什么地方出现问题了~~
请高手帮忙~~~
...全文
162 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinall 2005-12-14
  • 打赏
  • 举报
回复
szTemp=(char*)malloc(FileSize);
char* szBuffer=(char*)malloc(FileSize);
free(szTemp);
char* temp1=(char*)malloc(FileSize+200);
char* body=new char[FileSize+400];
free(temp1);
free(body);

可以看出问题有二:
1)char* szBuffer=(char*)malloc(FileSize);分配的内存没有释放
2)char* body=new char[FileSize+400];与free(body);不匹配,会造成“行为未定义”
xiaocai0001 2005-12-14
  • 打赏
  • 举报
回复
楼主怎么看出来有内存泄漏问题?

出什么错误提示了?

注意查看base64()函数中有没有可能是数组越界访问了.
sinall 2005-12-14
  • 打赏
  • 举报
回复
获得文件大小:
size_t size = 0;
FILE *pf;
if ((pf = fopen(strFileName.c_str(), "r")) != NULL)
{
fseek(pf, 0L, SEEK_END);
size = ftell(pf);
rewind(pf);
//……
fclose(pf);
}
csucdl 2005-12-14
  • 打赏
  • 举报
回复
内存泄露?
char* szBuffer=(char*)malloc(FileSize);//

mySmtp.base64(szTemp,szBuffer);
free(szTemp);
free(szBuffer); //
snowbirdfly 2005-12-14
  • 打赏
  • 举报
回复
改写成这样:
char filename[256];
char* szTemp;
wsprintf(filename,"%s",m_file);

FILE* hFile=fopen(filename,"a+");

//获得文件大小
DWORD FileSize=0;
char temp[2];
while(fread(temp,sizeof(char),1,hFile))
{
FileSize++;
}
fseek(hFile,0,0);

//读取文件内容
szTemp = new char[FileSize+1];
fread(szTemp,FileSize,1,hFile);

char* szBuffer=new char[FileSize+1];

//mySmtp.base64(szTemp,szBuffer);
delete []szTemp;
delete []szBuffer;

char* temp1 = new char[FileSize+201];
strcpy(temp1,"\r\nContent-Type:application/octet-stream;Name=%s\r\n");
strcat(temp1,"Content-Disposition:attachment;FileName=%s\r\n");
strcat(temp1,"Content-Transfer-Encoding:Base64\r\n\r\n");
strcat(temp1,"%s\r\n\r\n");

char* body=new char[FileSize+400];

char temp2[256];
strcpy(temp2,strtok(filename,"\\"));

strcpy(temp2,strtok(NULL,"\\"));
wsprintf(body,temp1,temp2,temp2,szBuffer);
strcat(sendinfo.msg,body);
delete []temp1;
delete []body;
没有问题~~
那问题只能是mySmtp.base64(szTemp,szBuffer);~~~
而mySmtp.base64为
int CSmtp::base64(char* s,char* d)
{
char CharSet[64]={
'A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X',
'Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3',
'4','5','6','7','8','9','+','/'};
unsigned char In[3];
unsigned char Out[4];
int cnt=0;

if(!s||!d) return 0;

for(;*s!=0;)
{
if(cnt+4>76)
{
*d++='\n';
cnt=0;
}
if(strlen(s)>=3)
{
In[0]=*s;
In[1]=*(s+1);
In[2]=*(s+2);
Out[0]=In[0]>>2;
Out[1]=(In[0]&0x03)<<4|(In[1]&0xf0)>>4;
Out[2]=(In[1]&0x0f)<<2|(In[2]&0xc0)>>6;
Out[3]=In[2]&0x3f;
*d=CharSet[Out[0]];
*(d+1)=CharSet[Out[1]];
*(d+2)=CharSet[Out[2]];
*(d+3)=CharSet[Out[3]];
s+=3;
d+=4;
}
else if(strlen(s)==1)
{
In[0]=*s;
Out[0]=In[0]>>2;
Out[1]=(In[0]&0x03)<<4|0;
*d=CharSet[Out[0]];
*(d+1)=CharSet[Out[1]];
*(d+2)='=';
*(d+3)='=';
s+=1;
d+=4;
}
else if(strlen(s)==2)
{
In[0]=*s;
In[1]=*(s+1);
Out[0]=In[0]>>2;
Out[1]=(In[0]&0x03)<<4|(In[1]&0xf0)>>4;
Out[2]=(In[1]&0x0f)<<2|0;
*d=CharSet[Out[0]];
*(d+1)=CharSet[Out[1]];
*(d+2)=CharSet[Out[2]];
*(d+3)='=';
s+=2;
d+=4;
}
cnt+=4;
}
*d='\0';
return 1;
}
这个是什么地方的问题呢???
cunsh 2005-12-14
  • 打赏
  • 举报
回复
malloc的比free的多一个呀.

那个szBuffer申请的内存

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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