Skip to content

Commit

Permalink
Avoid humpty-dumpty memory leak in the accessor model for repository.
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianfrasineanu committed Dec 3, 2016
1 parent adc572f commit 3ca8bce
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/Bootstrap.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Console.h"
#include <vld.h>

using namespace std;

Expand Down
7 changes: 6 additions & 1 deletion app/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ void Model::confirmInput(const map<string, string> &payLoad)

// Validate the input, if there are any errors, display them and reload the view.
this->repository->validateItems(this->truncatedInput);

delete this->repository;
}

void Model::render(const string &outputAlias)
Expand All @@ -67,6 +69,8 @@ void Model::render(const string &outputAlias)

truncatedAlias.erase(0, entityName.size() + 1);
this->repository->echo(truncatedAlias);

delete this->repository;
}

void Model::signalAction(const string &actionAlias)
Expand All @@ -78,9 +82,10 @@ void Model::signalAction(const string &actionAlias)

truncatedAlias.erase(0, entityName.size() + 1);
this->repository->apply(truncatedAlias);

delete this->repository;
}

Model::~Model()
{
delete this->repository;
}
6 changes: 1 addition & 5 deletions app/QuestionRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,7 @@ void QuestionRepository::validateItems(map<string, string> &truncatedInput)
{
if (truncatedInput.find("action")->second == "create")
{
this->users = new UserModel;
truncatedInput["userId"] = to_string(this->users->setActive().id);

// Prevent multiple IO streams opened at the same time.
delete this->users;
truncatedInput["userId"] = to_string(this->users.setActive().id);
}
this->receiveCleanInput(truncatedInput);
}
Expand Down
2 changes: 1 addition & 1 deletion app/QuestionRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class QuestionRepository : public RepositoryInterface {
static string alias;

QuestionModel model;
UserModel *users;
UserModel users;

void defineValidation();
void receiveCleanInput(map<string, string> &);
Expand Down

0 comments on commit 3ca8bce

Please sign in to comment.