什么是内存泄漏?具个简单的例子来看看~~!谢谢

zhong3113 2002-12-16 01:05:10
什么是内存泄漏?怎样会造成内存泄漏~!
具个简单的例子来看看~~!简单些好呀~!
...全文
379 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
realdreamer 2002-12-16
  • 打赏
  • 举报
回复
举个例子给你:


void main()
{
while(new char[1024])
;
}
zhou80bin 2002-12-16
  • 打赏
  • 举报
回复
内存泄漏
就是指分配的内存或资源
在使用以后,没有释放掉。
一般出现内存泄漏的情况
以new分配内存以后,没有用delete释放内存居多
wuchuncai 2002-12-16
  • 打赏
  • 举报
回复
char *ss;
ss=new char[100];

最后都没有delete []ss
puyinghua 2002-12-16
  • 打赏
  • 举报
回复
最简单的就是new分配内存后, 没delete 呵呵
qing_li73 2002-12-16
  • 打赏
  • 举报
回复
Furthermore , u can get the relative contents via MSDN

just paste a snippet here :

Detecting a Memory Leak
The following instructions and example show you how to detect a memory leak.

To detect a memory leak

Create a CMemoryState object and call the Checkpoint member function to get the initial snapshot of memory.


After you perform the memory allocation and deallocation operations, create another CMemoryState object and call Checkpoint for that object to get a current snapshot of memory usage.


Create a third CMemoryState object, call the Difference member function, and supply the previous two CMemoryState objects as arguments. The return value for the Difference function will be nonzero if there is any difference between the two specified memory states, indicating that some memory blocks have not been deallocated.
The following example shows how to check for memory leaks:

// Declare the variables needed
#ifdef _DEBUG
CMemoryState oldMemState, newMemState, diffMemState;
oldMemState.Checkpoint();
#endif

// do your memory allocations and deallocations...
CString s = "This is a frame variable";
// the next object is a heap object
CPerson* p = new CPerson( "Smith", "Alan", "581-0215" );

#ifdef _DEBUG
newMemState.Checkpoint();
if( diffMemState.Difference( oldMemState, newMemState ) )
{
TRACE( "Memory leaked!\n" );
}
#endif

Notice that the memory-checking statements are bracketed by #ifdef _DEBUG / #endif blocks so that they are compiled only in Win32 Debug versions of your program.
qing_li73 2002-12-16
  • 打赏
  • 举报
回复
See the sample below, FYI:

http://www.codeproject.com/useritems/leakfinder.asp
yarshray 2002-12-16
  • 打赏
  • 举报
回复
所有C++程序员对析构函数都不陌生,由于其简单且易理解,所以都能很快应用。这里我不说这些常用方法,若不知可参考C++书籍。而我这次所想说的是较微妙的技巧,常不被人注意,但却非常非常的重要。看以下代码:

//////////////////////////////////////////////////////
//Example 1
//author: 袁凯
//date: 2001-09-24
//////////////////////////////////////////////////////

#include <iostream.h>

class CFunction
{
public:
CFunction()
{
data = new char[64];
};
~CFunction()
{
delete [] data;
};
char *data;
};

class CFunctionEx : public CFunction
{
public:
CFunctionEx()
{
m_data = new char[64];
};
~CFunctionEx()
{
delete [] m_data;
};
private:
char *m_data;
};

void main()
{
CFunction *pCFun = new CFunctionEx;
delete pCFun;
}
你能看出什么问题吗?很显然,有内存泄漏。这是因为当删除pCFun时,它只调用了Cfunction的析构函数而没调用CfunctionEx的析构函数,所以导致内存泄漏。再看下例:
//\\////\\////\\////\\////\\////\\//\\////\\////\\//
//Example 2
//author: 袁凯
//date: 2001-09-24
//\\////\\////\\////\\////\\////\\//\\////\\////\\//
#include <iostream.h>
class CBase
{
public:
CBase()
{
data = new char[64];
};
~CBase()
{
delete [] data;
};
char *data;
};
class CFunction
{
public:
CFunction(){};
~CFunction(){};
};
class CFunctionEx : public CFunction
{
public:
CFunctionEx(){};
~CFunctionEx(){};
private:
CBase m_cbase;
};
void main()
{
CFunction *pCFun = new CFunctionEx;
delete pCFun;
}
你能看出什么问题吗?这里CfunctionEx和Cfunction中本身并没有分配内存,应该不会有内存泄漏。和上例一样当删除pCFun时,它只调用了Cfunction的析构函数而没调用CfunctionEx的析构函数,但CfunctionEx本身并没分配内存,是什么地方有内存泄漏我不说大家也应该知道了吧。不错是m_cbase,因为它是Cbase的实例且是CfunctionEx成员变量,当CfunctionEx的的析构函数没有被调用时,当然m_cbase的析构函数也没有被调用,所以Cbase中分配的内存被泄漏。
解决以上问题的方法很简单,就是使基类Cfunction的析构函数为虚函数就可以了。很简单,是吗?哈哈……
这样就得出一个结论,当你的基类的析构函数不为虚的话,
1.1 其子类中所分配的内存将可能泄漏。
2.2 其子类中所有的成员变量的类中分配的内存也将可能泄漏。
第二点非常重要,因为很容易被遗漏。我就是为此这才写此文。
这里说的可能是因为,如果程序中没有以上示例类似写法(指用基类指针指向子类实例裕,虚函数是C++的精华,很少有人不用的,由其是在大中型软件项目中),就不会出现本文所说的内存泄漏。看来在基类中使析构函数为虚函数是如此的重要。所以强烈建议在基类中把析构函数声明为虚函数,但是只有你写的类并不做为基类时例外。

16,473

社区成员

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

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

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