bugfix: fix memory leak issue for strategyPattern

master
FengJungle 2021-10-15 07:49:32 +08:00
parent 7ca6b5fcc9
commit 6c2fadfa54
1 changed files with 16 additions and 1 deletions

View File

@ -9,14 +9,29 @@ class Context
{
public:
Context(){
arr = NULL;
arr = nullptr;
N = 0;
sortStrategy = nullptr;
}
Context(int iArr[], int iN){
this->arr = iArr;
this->N = iN;
sortStrategy = nullptr;
}
~Context()
{
if(sortStrategy)
{
delete sortStrategy;
sortStrategy = nullptr;
}
}
void setSortStrategy(Strategy* iSortStrategy){
if(sortStrategy)
{
delete sortStrategy;
sortStrategy = nullptr;
}
this->sortStrategy = iSortStrategy;
}
void sort(){