Browse Source

Fixed all memory leaks

iam 1 year ago
parent
commit
abfbdfac23

BIN
build/checkCondition.o


BIN
build/gameController.o


BIN
build/main.o


BIN
build/option.o


BIN
build/storyline.o


BIN
game


+ 2 - 1
includes/gameController.h

@@ -32,6 +32,7 @@ class GameController
32 32
     public:
33 33
     
34 34
     GameController();
35
+    ~GameController();
35 36
     
36 37
     PlayerInfo player;
37 38
     
@@ -58,7 +59,7 @@ class GameController
58 59
     bool parseFile(std::string fileName);
59 60
     int addStoryline(std::string storylineName, std::string storylineData); //return index of new storyline
60 61
     
61
-    void sendInput(std::string input);
62
+    bool sendInput(std::string input);
62 63
     void getOptions(std::vector<std::string> &toReturn);
63 64
     
64 65
     bool parseLine(std::string line, std::string currentCondition, std::vector<Command*> &commands); //line parsing here so it can be accessed by options and storylines

+ 1 - 0
includes/option.h

@@ -25,6 +25,7 @@ class Option {
25 25
     std::string condition;
26 26
     
27 27
     Option(std::string data, int optionNumber_, std::string optionTitle_, std::string condition_, GameController* currentGame_);
28
+    ~Option();
28 29
     
29 30
 };
30 31
 

+ 1 - 0
includes/storyline.h

@@ -26,6 +26,7 @@ class Storyline
26 26
     
27 27
     std::string storylineName;
28 28
     Storyline(std::string data, std::string storylineName, GameController* currentGame_);
29
+    ~Storyline();
29 30
     std::string getOption(std::string userInput, std::vector<Option*> &chooseFrom);
30 31
     void displayOptions(std::vector<Option*> &chooseFrom, std::vector<std::string> &returnOptions);
31 32
 };

+ 13 - 2
src/gameController.cpp

@@ -19,13 +19,21 @@ GameController::GameController()
19 19
     gameClock = 0;
20 20
 }
21 21
 
22
+GameController::~GameController()
23
+{
24
+    for(auto i : storylines)
25
+        delete i;
26
+    
27
+    storylines.clear();
28
+}
29
+
22 30
 int GameController::addStoryline(std::string storylineName, std::string storylineData)
23 31
 {
24 32
     storylines.push_back(new Storyline(storylineData, storylineName, this));
25 33
     return 0;
26 34
 }
27 35
 
28
-void GameController::sendInput(std::string input)
36
+bool GameController::sendInput(std::string input)
29 37
 {
30 38
     std::string returnString;
31 39
     
@@ -37,10 +45,13 @@ void GameController::sendInput(std::string input)
37 45
             if(returnString[0] != ';')
38 46
             {
39 47
                 std::cout << returnString << std::endl;
40
-                return;
48
+                return 0;
41 49
             }
42 50
             else
51
+            {
43 52
                 storyline = returnString;
53
+                return 1;
54
+            }
44 55
         }
45 56
 }
46 57
 

+ 17 - 6
src/main.cpp

@@ -39,7 +39,7 @@ std::string getUserInput(std::vector<GameContainer*> *mainGameVector, int *gameI
39 39
         
40 40
         std::getline(std::cin,userInput);
41 41
         
42
-        //userInput += " MOO";
42
+        //userInput += " moo";
43 43
         
44 44
         gameNameIndex = userInput.find_last_of(" ");
45 45
         gameName = userInput.substr(gameNameIndex+1, userInput.size());
@@ -94,18 +94,29 @@ int main()
94 94
     mainGameVector.at(0)->game->storyline = ";setup;";
95 95
     printOptions(0, &mainGameVector);
96 96
     
97
+    mainGameVector.push_back(new GameContainer);
98
+    mainGameVector.at(1)->game = new GameController();
99
+    mainGameVector.at(1)->sessionKey = "moo2";
100
+    mainGameVector.at(1)->game->parseFile("game.txt");
101
+    mainGameVector.at(1)->game->storyline = ";setup;";
102
+    printOptions(1, &mainGameVector);
103
+    
97 104
     std::string userInput;
98 105
     int gameIndex;
99 106
     
100
-    while(true)
107
+    while(mainGameVector.size() > 0)
101 108
     {
102 109
         usleep(100);
103 110
         
104
-        //make closing games work
105
-        
106 111
         userInput = getUserInput(&mainGameVector, &gameIndex);
107
-        mainGameVector.at(gameIndex)->game->sendInput(userInput);
108
-        printOptions(gameIndex, &mainGameVector);
112
+        if(!mainGameVector.at(gameIndex)->game->sendInput(userInput))
113
+        {
114
+            delete mainGameVector.at(gameIndex)->game;
115
+            delete mainGameVector.at(gameIndex);
116
+            mainGameVector.erase(mainGameVector.begin() + gameIndex);
117
+        }
118
+        else
119
+            printOptions(gameIndex, &mainGameVector);
109 120
     }
110 121
     
111 122
     return 0;

+ 8 - 0
src/option.cpp

@@ -5,6 +5,14 @@
5 5
 #include "option.h"
6 6
 #include "extras.h"
7 7
 
8
+Option::~Option()
9
+{
10
+    for(auto i : commands)
11
+        delete i;
12
+    
13
+    commands.clear();
14
+}
15
+
8 16
 Option::Option(std::string data, int optionNumber_, std::string optionTitle_, std::string condition_, GameController *currentGame_) :
9 17
     optionNumber(optionNumber_),
10 18
     optionTitle(optionTitle_),

+ 13 - 0
src/storyline.cpp

@@ -7,6 +7,19 @@
7 7
 #include "extras.h"
8 8
 #include "option.h"
9 9
 
10
+Storyline::~Storyline()
11
+{
12
+    for(auto i : options)
13
+        delete i;
14
+    
15
+    options.clear();
16
+    
17
+    for(auto i : commands)
18
+        delete i;
19
+    
20
+    commands.clear();
21
+}
22
+
10 23
 Storyline::Storyline(std::string data_, std::string storylineName_, GameController *currentGame_):
11 24
     currentGame(currentGame_),
12 25
     data(data_),