为什么我的代码会有内存泄露#include iostream#include afx.h//#include common.husing namespace s
为什么我的代码会有内存泄露
#include <iostream>
#include <afx.h>
//#include "common.h"
using namespace std;
class parent
{
public:
int age;
parent ()
{}
parent(int age)
{
this->age = age;
cout<<"parent construct!"<<endl;
};
int getAge()
{ return age;}
};
class son : public parent
{
public:
son(int age)
{
this->age = age;
cout<<"son construct!"<<endl;
}
};
class girl : public parent
{
public:
girl(int age)
{
this->age = age;
cout<<"girl construnt!"<<endl;
}
};
class factory
{
public:
factory()
{}
static parent* getInstance(const char* str)
{
if (strcmp(str, "son") == 0)
{
return new son(15);
}
else if (strcmp(str, "girl") == 0)
{
return new girl(13);
}
}
};
int main()
{
parent* ins = NULL;
ins = factory::getInstance("son");
cout<<ins->getAge()<<endl;
delete ins;
ins = NULL;
_CrtDumpMemoryLeaks();
system("pause");
};
求高手帮我看看,为什么最后会显示内存泄露
[解决办法]
没看出有泄漏的地方.
[解决办法]
同样没看出问题。
[解决办法]
工厂模式中:
你的这个int getAge() { return age;} 请用virtual修饰一下。
执行不会出现内存泄露 ,你那的错误提示是什么
[解决办法]
检查是否资源泄漏的办法之一:
在任务管理器 进程 查看 选择列 里面选择:内存使用、虚拟内存大小、句柄数、线程数、USER对象、GDI对象
让你的程序(进程)不退出,循环执行主流程很多遍,越多越好,比如1000000次甚至无限循环,记录以上各数值,再隔至少一小时,越长越好,比如一个月,再记录以上各数值。如果以上两组数值的差较大或随时间流逝不断增加,则铁定有对应资源的资源泄漏!
