Commit 5603edaf authored by ahmedaj's avatar ahmedaj
Browse files

Fixed memory leaks

parent a9c66a6e
CC = g++
CC = g++ -g
FLAGS = -std=c++11
RACERDIR =
......
......@@ -145,6 +145,10 @@ RandomForest::getForestPrediction(vector <string> test, RandomForest *forest, ve
sort(reports, test, features);
for (int j = 0; j<explanations.size(); j++){
delete explanations[j];
}
return predictions;
}
......
......@@ -11,6 +11,7 @@
#include <bits/stdc++.h>
#include "helpers.hpp"
#include "DecisionTree.hpp"
#include "Node.hpp"
using namespace std::chrono;
......@@ -158,10 +159,11 @@ void splitTrainingAndTesting(vector<int> trainingIndecies, vector <vector<string
trainingDataString.push_back(emptyStringVector);
testDataString.push_back(emptyStringVector);
}
int trainingSize = trainingIndecies.size();
int currentTrainingIndex = trainingIndecies.back();
trainingIndecies.pop_back();
int numDataEntries = dataString.at(0).size();
int trainingCount = 1;
for (int dataIdx = numDataEntries - 1; dataIdx >= 0; dataIdx--) {
if (currentTrainingIndex == dataIdx) {
......@@ -169,8 +171,12 @@ void splitTrainingAndTesting(vector<int> trainingIndecies, vector <vector<string
for (int featureIdx = 0; featureIdx < dataString.size(); featureIdx++) {
trainingDataString.at(featureIdx).push_back(dataString.at(featureIdx).at(dataIdx));
}
currentTrainingIndex = trainingIndecies.back();
trainingIndecies.pop_back();
if(trainingCount<trainingSize){
currentTrainingIndex = trainingIndecies.back();
trainingIndecies.pop_back();
trainingCount ++;
}
} else {
for (int featureIdx = 0; featureIdx < dataString.size(); featureIdx++) {
testDataString.at(featureIdx).push_back(dataString.at(featureIdx).at(dataIdx));
......@@ -354,7 +360,10 @@ BestSplitPoint findBestSplit(double parentEntropy, int currentDepth, vector <vec
rightLabelCount[data[data.size() - 1][featureData.at(indx).first]] -= 1;
indx++;
dataIndex = featureData[indx].first;
if(indx <featureData.size()){
dataIndex = featureData[indx].first;
}
}
if (indx == featureData.size()) { //TODO check
continue;
......@@ -516,4 +525,17 @@ vector <int> oversample(vector <vector<string>> &data, vector<int> &indices) {
return toAdd;
}
void cleanTree(Node *node){
if(node->isLeaf){
delete node->question;
delete node;
return;
}
cleanTree(node->trueBranch);
cleanTree(node->falseBranch);
delete node->question;
delete node;
}
\ No newline at end of file
......@@ -4,6 +4,7 @@
#include <iterator>
#include <bits/stdc++.h>
#include "util.hpp"
#include "Node.hpp"
#ifndef HELPERS_HPP
#define HELPERS_HPP
......@@ -44,5 +45,6 @@ vector<int> randomSelect_WithoutReplacement(int originalNum, float percentTraini
vector<int> oversample(vector<vector<string>> &data, vector<int> &indices);
BestSplitPoint findBestSplit(double parentEntropy, int currentDepth, vector <vector<string>> &data,
vector <FeatureType> featureType, float featureWeight, vector<int>& nodeDatasetIndices );
void cleanTree(Node *node);
#endif
\ No newline at end of file
......@@ -100,6 +100,13 @@ int main(int argc, char *argv[]) {
randomForest->getForestPrediction(testData, randomForest, features);
for (int i = 0; i<randomForest->trees.size(); i++){
cleanTree(randomForest->trees[i]->root);
delete randomForest->trees[i];
}
delete randomForest;
}
ofstream outfile;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment