fstream 的内存泄漏

duoduo_uk 2008-05-07 09:32:15
急问: 当使用ifstream/ofstream发现有内存泄漏。我使用的是vc6。后来从微软网站上当了service pack 5/6装上。发现内存泄漏依然存在。烦请高人指点一下迷津。
以下是测试代码:

#include "stdafx.h"
#include <crtdbg.h>

#include <fstream>
#include <iostream>
#include <strstream>

using namespace std ;

int main(int argc, char* argv[])
{

_CrtMemState s01, s02, s03;

_CrtMemCheckpoint( &s01 );


printf("Hello World!\n");


ifstream strm("test.txt",ios::in);
if (!strm)
{
cout<<"Cannot open "<<"test.txt"<<endl;
}
strm.close();



_CrtMemCheckpoint( &s02 );


if ( _CrtMemDifference( &s03, &s01, &s02) )
{
printf("leaking\n");
_CrtMemDumpStatistics( &s03 );
}
else
printf("memory check done!\n");

return 0;
}



这里是输出:
C:\temp\test1\Debug>test1
Hello World!
leaking

同样的代码如果用vc++2005 express edition编译,则没有问题。
烦请高人指点一下。。。
...全文
340 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

ifstream strm("test.txt",ios::in);
if (!strm)
{
cout<<"Cannot open "<<"test.txt"<<endl;
}
strm.close();

就好了,这不是内存泄露

65,213

社区成员

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

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