bugfix: fix memory leak issue

master
FengJungle 2021-10-13 23:08:03 +08:00
parent c103bb8897
commit a55f692048
1 changed files with 18 additions and 6 deletions

View File

@ -2,6 +2,9 @@
#define __INTERPRETOR_PATTERN_H__ #define __INTERPRETOR_PATTERN_H__
#include <vector> #include <vector>
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std; using namespace std;
// ³éÏó±í´ïʽÀà // ³éÏó±í´ïʽÀà
@ -87,17 +90,17 @@ public:
this->input = iInput; this->input = iInput;
} }
void handle(){ void handle(){
AbstractNode *left = NULL; AbstractNode *left = nullptr;
AbstractNode *right = NULL; AbstractNode *right = nullptr;
AbstractNode *op = NULL; AbstractNode *op = nullptr;
AbstractNode *sentence = NULL; AbstractNode *sentence = nullptr;
string iInput = this->input; string iInput = this->input;
vector<string>inputList; vector<string>inputList;
char* inputCh = const_cast<char*>(iInput.c_str()); char* inputCh = const_cast<char*>(iInput.c_str());
char *token = strtok(inputCh, " "); char *token = strtok(inputCh, " ");
while (token != NULL){ while (token != nullptr){
inputList.push_back(token); inputList.push_back(token);
token = strtok(NULL, " "); token = strtok(nullptr, " ");
} }
for (int i = 0; i < inputList.size() - 2; i += 2){ for (int i = 0; i < inputList.size() - 2; i += 2){
left = new ValueNode(*(inputList[i].c_str())); left = new ValueNode(*(inputList[i].c_str()));
@ -105,6 +108,15 @@ public:
right = new ValueNode(*(inputList[i+2].c_str())); right = new ValueNode(*(inputList[i+2].c_str()));
sentence = new SentenceNode(left, right, op); sentence = new SentenceNode(left, right, op);
inputList[i + 2] = string(1, sentence->interpret()); inputList[i + 2] = string(1, sentence->interpret());
delete left;
delete right;
delete op;
delete sentence;
left = nullptr;
right = nullptr;
op = nullptr;
sentence = nullptr;
} }
string tmpRes = inputList[inputList.size() - 1]; string tmpRes = inputList[inputList.size() - 1];
if (tmpRes == "1"){ if (tmpRes == "1"){