fix memory leak

master
FengJungle 2020-11-29 15:28:46 +08:00
parent ed859f709e
commit 80c928a497
19 changed files with 118 additions and 19 deletions

BIN
.vs/DesignPattern/v15/.suo Normal file

Binary file not shown.

Binary file not shown.

3
.vs/ProjectSettings.json Normal file
View File

@ -0,0 +1,3 @@
{
"CurrentProjectSetting": "无配置"
}

18
.vs/VSWorkspaceState.json Normal file
View File

@ -0,0 +1,18 @@
{
"ExpandedNodes": [
"",
"\\02.FactoryMethod",
"\\02.FactoryMethod\\2.Code",
"\\03.AbstractFactory",
"\\03.AbstractFactory\\2.Code",
"\\04.BuilderPattern",
"\\04.BuilderPattern\\2.Code",
"\\05.PrototypePattern",
"\\05.PrototypePattern\\2.Code",
"\\06.Singleton",
"\\06.Singleton\\2.Code",
"\\06.Singleton\\2.Code\\Singleton"
],
"SelectedNode": "\\06.Singleton\\2.Code\\Singleton\\main.cpp",
"PreviewInSolutionExplorer": false
}

BIN
.vs/slnx.sqlite Normal file

Binary file not shown.

View File

@ -10,11 +10,21 @@ int main()
AbstractSportProduct *product = NULL; AbstractSportProduct *product = NULL;
product = fac->getSportProduct("Basketball"); product = fac->getSportProduct("Basketball");
if (product) {
delete product;
}
product = fac->getSportProduct("Football"); product = fac->getSportProduct("Football");
if (product) {
delete product;
}
product = fac->getSportProduct("Volleyball"); product = fac->getSportProduct("Volleyball");
if (product) {
delete product;
}
delete fac;
system("pause"); system("pause");
return 0; return 0;
} }

View File

@ -11,12 +11,33 @@ int main()
fac = new BasketballFactory(); fac = new BasketballFactory();
product = fac->getSportProduct(); product = fac->getSportProduct();
if (fac)
{
delete fac;
}
if (product) {
delete product;
}
fac = new FootballFactory(); fac = new FootballFactory();
product = fac->getSportProduct(); product = fac->getSportProduct();
if (fac)
{
delete fac;
}
if (product) {
delete product;
}
fac = new VolleyballFactory(); fac = new VolleyballFactory();
product = fac->getSportProduct(); product = fac->getSportProduct();
if (fac)
{
delete fac;
}
if (product) {
delete product;
}
system("pause"); system("pause");
return 0; return 0;

View File

@ -10,10 +10,16 @@ int main()
fac = new BasketballFactory(); fac = new BasketballFactory();
ball = fac->getBall(); ball = fac->getBall();
shirt = fac->getShirt(); shirt = fac->getShirt();
delete fac;
delete ball;
delete shirt;
fac = new FootballFactory(); fac = new FootballFactory();
ball = fac->getBall(); ball = fac->getBall();
shirt = fac->getShirt(); shirt = fac->getShirt();
delete fac;
delete ball;
delete shirt;
system("pause"); system("pause");
return 0; return 0;

View File

@ -9,18 +9,18 @@ using namespace std;
class House class House
{ {
public: public:
House(){} House() {}
void setFloor(string iFloor){ void setFloor(string iFloor) {
this->floor = iFloor; this->floor = iFloor;
} }
void setWall(string iWall){ void setWall(string iWall) {
this->wall = iWall; this->wall = iWall;
} }
void setRoof(string iRoof){ void setRoof(string iRoof) {
this->roof = iRoof; this->roof = iRoof;
} }
//打印House信息 //打印House信息
void printfHouseInfo(){ void printfHouseInfo() {
printf("Floor:%s\t\n", this->floor.c_str()); printf("Floor:%s\t\n", this->floor.c_str());
printf("Wall:%s\t\n", this->wall.c_str()); printf("Wall:%s\t\n", this->wall.c_str());
printf("Roof:%s\t\n", this->roof.c_str()); printf("Roof:%s\t\n", this->roof.c_str());
@ -35,9 +35,17 @@ private:
class AbstractBuilder class AbstractBuilder
{ {
public: public:
AbstractBuilder(){ AbstractBuilder() {
house = new House(); house = new House();
} }
virtual ~AbstractBuilder()
{
if (house != nullptr)
{
delete house;
house = nullptr;
}
}
//抽象方法: //抽象方法:
virtual void buildFloor() = 0; virtual void buildFloor() = 0;
virtual void buildWall() = 0; virtual void buildWall() = 0;
@ -51,20 +59,28 @@ public:
class ConcreteBuilderA :public AbstractBuilder class ConcreteBuilderA :public AbstractBuilder
{ {
public: public:
ConcreteBuilderA(){ ConcreteBuilderA() {
printf("ConcreteBuilderA\n"); printf("ConcreteBuilderA\n");
} }
~ConcreteBuilderA()
{
if (this->house != nullptr)
{
delete house;
house = nullptr;
}
}
//具体实现方法 //具体实现方法
void buildFloor(){ void buildFloor() {
this->house->setFloor("Floor_A"); this->house->setFloor("Floor_A");
} }
void buildWall(){ void buildWall() {
this->house->setWall("Wall_A"); this->house->setWall("Wall_A");
} }
void buildRoof(){ void buildRoof() {
this->house->setRoof("Roof_A"); this->house->setRoof("Roof_A");
} }
House *getHouse(){ House *getHouse() {
return this->house; return this->house;
} }
}; };
@ -73,20 +89,28 @@ public:
class ConcreteBuilderB :public AbstractBuilder class ConcreteBuilderB :public AbstractBuilder
{ {
public: public:
ConcreteBuilderB(){ ConcreteBuilderB() {
printf("ConcreteBuilderB\n"); printf("ConcreteBuilderB\n");
} }
~ConcreteBuilderA()
{
if (this->house != nullptr)
{
delete house;
house = nullptr;
}
}
//具体实现方法 //具体实现方法
void buildFloor(){ void buildFloor() {
this->house->setFloor("Floor_B"); this->house->setFloor("Floor_B");
} }
void buildWall(){ void buildWall() {
this->house->setWall("Wall_B"); this->house->setWall("Wall_B");
} }
void buildRoof(){ void buildRoof() {
this->house->setRoof("Roof_B"); this->house->setRoof("Roof_B");
} }
House *getHouse(){ House *getHouse() {
return this->house; return this->house;
} }
}; };
@ -95,12 +119,20 @@ public:
class Director class Director
{ {
public: public:
Director(){} Director() :builder(nullptr) {}
~Director()
{
if (this->builder != nullptr)
{
delete builder;
builder = nullptr;
}
}
//具体实现方法 //具体实现方法
void setBuilder(AbstractBuilder *iBuilder){ void setBuilder(AbstractBuilder *iBuilder) {
this->builder = iBuilder; this->builder = iBuilder;
} }
House *construct(){ House *construct() {
builder->buildFloor(); builder->buildFloor();
builder->buildWall(); builder->buildWall();
builder->buildRoof(); builder->buildRoof();

View File

@ -14,6 +14,7 @@ int main()
director->setBuilder(builder); director->setBuilder(builder);
house = director->construct(); house = director->construct();
house->printfHouseInfo(); house->printfHouseInfo();
delete builder;
//寧땍야竟쉔芚諒B //寧땍야竟쉔芚諒B
builder = new ConcreteBuilderB(); builder = new ConcreteBuilderB();
@ -22,5 +23,9 @@ int main()
house->printfHouseInfo(); house->printfHouseInfo();
system("pause"); system("pause");
delete director;
delete builder;
return 0; return 0;
} }

View File

@ -48,5 +48,9 @@ int main()
jungleWork->printWorkInfo(); jungleWork->printWorkInfo();
system("pause"); system("pause");
delete singleWork;
delete jungleModel;
delete jungleWork;
return 0; return 0;
} }