Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fix some mem leaks

  • Loading branch information...
commit d35d0b7abeeae7a9385a4701c460dab3c28a307b 1 parent 0946162
zerver zerver authored
3  rts/Game/Game.cpp
@@ -356,6 +356,7 @@ CGame::~CGame()
356 356 teamHandler->Team(t)->Died(false);
357 357 }
358 358
  359 + CEndGameBox::Destroy();
359 360 CLoadScreen::DeleteInstance(); // make sure to halt loading, otherwise crash :)
360 361
361 362 // TODO move these to the end of this dtor, once all action-executors are registered by their respective engine sub-parts
@@ -1918,7 +1919,7 @@ void CGame::GameEnd(const std::vector<unsigned char>& winningAllyTeams, bool tim
1918 1919 gameOver = true;
1919 1920 eventHandler.GameOver(winningAllyTeams);
1920 1921
1921   - new CEndGameBox(winningAllyTeams);
  1922 + CEndGameBox::Create(winningAllyTeams);
1922 1923 #ifdef HEADLESS
1923 1924 profiler.PrintProfilingInfo();
1924 1925 #endif // HEADLESS
3  rts/Game/PreGame.cpp
@@ -25,6 +25,7 @@
25 25
26 26 #include "aGui/Gui.h"
27 27 #include "ExternalAI/SkirmishAIHandler.h"
  28 +#include "Menu/SelectMenu.h"
28 29 #include "Rendering/glFont.h"
29 30 #include "Sim/Misc/GlobalSynced.h"
30 31 #include "Sim/Misc/GlobalConstants.h"
@@ -53,6 +54,7 @@ using std::string;
53 54 CONFIG(bool, DemoFromDemo).defaultValue(false);
54 55
55 56 CPreGame* pregame = NULL;
  57 +extern SelectMenu* selectMenu;
56 58
57 59 CPreGame::CPreGame(const ClientSetup* setup) :
58 60 settings(setup),
@@ -78,6 +80,7 @@ CPreGame::~CPreGame()
78 80 {
79 81 // don't delete infoconsole, its beeing reused by CGame
80 82 agui::gui->Draw(); // delete leftover gui elements (remove once the gui is drawn ingame)
  83 + selectMenu = NULL;
81 84
82 85 pregame = NULL;
83 86 }
4 rts/Game/UI/EndGameBox.cpp
@@ -42,7 +42,7 @@ static std::string FloatToSmallString(float num, float mul = 1) {
42 42
43 43
44 44 bool CEndGameBox::enabled = true;
45   -
  45 +CEndGameBox* CEndGameBox::endGameBox = NULL;
46 46
47 47 CEndGameBox::CEndGameBox(const std::vector<unsigned char>& winningAllyTeams)
48 48 : CInputReceiver()
@@ -53,6 +53,7 @@ CEndGameBox::CEndGameBox(const std::vector<unsigned char>& winningAllyTeams)
53 53 , winners(winningAllyTeams)
54 54 , graphTex(0)
55 55 {
  56 + endGameBox = this;
56 57 box.x1 = 0.14f;
57 58 box.y1 = 0.1f;
58 59 box.x2 = 0.86f;
@@ -88,6 +89,7 @@ CEndGameBox::~CEndGameBox()
88 89 if (graphTex) {
89 90 glDeleteTextures(1,&graphTex);
90 91 }
  92 + endGameBox = NULL;
91 93 }
92 94
93 95 bool CEndGameBox::MousePress(int x, int y, int button)
3  rts/Game/UI/EndGameBox.h
@@ -24,8 +24,11 @@ class CEndGameBox : public CInputReceiver
24 24 virtual std::string GetTooltip(int x, int y);
25 25
26 26 static bool enabled;
  27 + static void Create(const std::vector<unsigned char>& winningAllyTeams) { if (endGameBox == NULL) new CEndGameBox(winningAllyTeams);}
  28 + static void Destroy() { if (endGameBox != NULL) delete endGameBox; }
27 29
28 30 protected:
  31 + static CEndGameBox* endGameBox;
29 32 void FillTeamStats();
30 33 ContainerBox box;
31 34
1  rts/Rendering/Env/AdvWater.cpp
@@ -347,6 +347,7 @@ void CAdvWater::UpdateWater(CGame* game)
347 347 // camera = realCam;
348 348 camera->~CCamera();
349 349 new (camera) CCamera(*(reinterpret_cast<CCamera*>(realCam)));
  350 + reinterpret_cast<CCamera*>(realCam)->~CCamera();
350 351
351 352 camera->Update();
352 353 glPopAttrib();
1  rts/Rendering/Env/BumpWater.cpp
@@ -1260,6 +1260,7 @@ void CBumpWater::DrawReflection(CGame* game)
1260 1260 // camera = realCam;
1261 1261 camera->~CCamera();
1262 1262 new (camera) CCamera(*(reinterpret_cast<CCamera*>(realCam)));
  1263 + reinterpret_cast<CCamera*>(realCam)->~CCamera();
1263 1264 camera->Update();
1264 1265 }
1265 1266
1  rts/Rendering/Env/CubeMapHandler.cpp
@@ -220,6 +220,7 @@ void CubeMapHandler::CreateReflectionFace(unsigned int glType, const float3& cam
220 220
221 221 camera->~CCamera();
222 222 new (camera) CCamera(*reinterpret_cast<CCamera*>(cameraMemBuf));
  223 + reinterpret_cast<CCamera*>(cameraMemBuf)->~CCamera();
223 224 camera->Update();
224 225 }
225 226
15 rts/Rendering/Models/IModelParser.cpp
@@ -95,10 +95,14 @@ C3DModelLoader::C3DModelLoader()
95 95 C3DModelLoader::~C3DModelLoader()
96 96 {
97 97 // delete model cache
98   - for (std::list<S3DModel*>::iterator mi = models.begin(); mi != models.end(); ++mi) {
99   - S3DModel* model = *mi;
100   - model->DeletePieces(model->GetRootPiece());
101   - delete model;
  98 + std::set<S3DModel*> deletedModels;
  99 + for (ModelMap::iterator mi = cache.begin(); mi != cache.end(); ++mi) {
  100 + S3DModel* model = mi->second;
  101 + if (deletedModels.find(model) == deletedModels.end()) {
  102 + model->DeletePieces(model->GetRootPiece());
  103 + delete model;
  104 + deletedModels.insert(model);
  105 + }
102 106 }
103 107
104 108 // get rid of Spring's native parsers
@@ -240,8 +244,7 @@ S3DModel* C3DModelLoader::Load3DModel(std::string name)
240 244
241 245 cache[name] = model; // cache the model
242 246 cache[modelPath] = model; // cache the model
243   - models.push_back(model);
244   - model->id = models.size(); // IDs start with 1
  247 + model->id = cache.size(); // IDs start with 1
245 248
246 249 return model;
247 250 }
1  rts/Rendering/Models/IModelParser.h
@@ -37,7 +37,6 @@ class C3DModelLoader
37 37 private:
38 38 ModelMap cache;
39 39 ParserMap parsers;
40   - std::list<S3DModel*> models;
41 40
42 41 std::list<S3DModelPiece*> createLists;
43 42
5 rts/Sim/Misc/DefinitionTag.cpp
@@ -19,6 +19,11 @@ using std::string;
19 19 LOG_L(L_ERROR, "%s:%d: " fmt, data->GetDeclarationFile().Get().c_str(), data->GetDeclarationLine().Get(), ## __VA_ARGS__) \
20 20
21 21
  22 +DefType::~DefType() {
  23 + for (MetaDataMap::const_iterator it = map.begin(); it != map.end(); ++it)
  24 + delete it->second;
  25 +}
  26 +
22 27 std::map<std::string, const DefType*>& DefType::GetTypes()
23 28 {
24 29 static std::map<std::string, const DefType*> tagtypes;
1  rts/Sim/Misc/DefinitionTag.h
@@ -217,6 +217,7 @@ class DefType
217 217 {
218 218 public:
219 219 DefType(const std::string& name);
  220 + virtual ~DefType();
220 221
221 222 template<typename T> DefTagBuilder<T> AddTag(const char* name) {
222 223 DefTagTypedMetaData<T>* meta = new DefTagTypedMetaData<T>(name);
6 rts/Sim/Projectiles/ProjectileHandler.cpp
@@ -10,6 +10,7 @@
10 10 #include "Map/MapInfo.h"
11 11 #include "Rendering/GlobalRendering.h"
12 12 #include "Rendering/GroundFlash.h"
  13 +#include "Rendering/Models/S3OParser.h" // for delete [] SS3OVertex*
13 14 #include "Sim/Features/Feature.h"
14 15 #include "Sim/Features/FeatureDef.h"
15 16 #include "Sim/Misc/CollisionHandler.h"
@@ -535,8 +536,11 @@ void CProjectileHandler::AddFlyingPiece(int team, float3 pos, float3 speed, cons
535 536
536 537 void CProjectileHandler::AddFlyingPiece(int textureType, int team, float3 pos, float3 speed, SS3OVertex* verts)
537 538 {
538   - if (textureType <= 0)
  539 + if (textureType <= 0) {
  540 + if (verts != NULL)
  541 + delete [] verts;
539 542 return; // texture 0 means 3do
  543 + }
540 544
541 545 FlyingPiece* fp = new FlyingPiece(team, pos, speed, textureType, verts);
542 546 flyingPiecesS3O.insert(fp);
22 rts/Sim/Units/Scripts/CobEngine.cpp
@@ -37,18 +37,26 @@ CCobEngine::CCobEngine()
37 37 CCobEngine::~CCobEngine()
38 38 {
39 39 //Should delete all things that the scheduler knows
40   - for (std::list<CCobThread *>::iterator i = running.begin(); i != running.end(); ++i) {
41   - delete *i;
  40 + while (!running.empty()) {
  41 + CCobThread *tmp = running.front();
  42 + tmp->SetCallback(NULL, NULL, NULL);
  43 + running.pop_front();
  44 + delete tmp;
42 45 }
43   - for (std::list<CCobThread *>::iterator i = wantToRun.begin(); i != wantToRun.end(); ++i) {
44   - delete *i;
  46 + while(!wantToRun.empty()) {
  47 + CCobThread *tmp = wantToRun.front();
  48 + tmp->SetCallback(NULL, NULL, NULL);
  49 + wantToRun.pop_front();
  50 + delete tmp;
45 51 }
46   - while (sleeping.size() > 0) {
47   - CCobThread *tmp;
48   - tmp = sleeping.top();
  52 + while (!sleeping.empty()) {
  53 + CCobThread *tmp = sleeping.top();
  54 + tmp->SetCallback(NULL, NULL, NULL);
49 55 sleeping.pop();
50 56 delete tmp;
51 57 }
  58 +
  59 + assert(running.empty() && wantToRun.empty() && sleeping.empty());
52 60 }
53 61
54 62
7 rts/System/Input/Joystick.cpp
@@ -33,6 +33,13 @@ void InitJoystick()
33 33 };
34 34 }
35 35
  36 +void FreeJoystick() {
  37 + if (stick != NULL) {
  38 + delete stick;
  39 + stick = NULL;
  40 + }
  41 +}
  42 +
36 43 Joystick::Joystick()
37 44 {
38 45 const int numSticks = SDL_NumJoysticks();
1  rts/System/Input/Joystick.h
@@ -9,6 +9,7 @@
9 9 union SDL_Event;
10 10
11 11 void InitJoystick();
  12 +void FreeJoystick();
12 13
13 14 class Joystick
14 15 {
3  rts/System/MemPool.cpp
@@ -24,6 +24,7 @@ void* CMemPool::Alloc(size_t numBytes)
24 24 nextFree[numBytes] = (*(void**)pnt);
25 25 } else {
26 26 void* newBlock = ::operator new(numBytes * poolSize[numBytes]);
  27 + allocated.push_back(newBlock);
27 28 for (int i = 0; i < (poolSize[numBytes] - 1); ++i) {
28 29 *(void**)&((char*)newBlock)[(i) * numBytes] = (void*)&((char*)newBlock)[(i + 1) * numBytes];
29 30 }
@@ -54,4 +55,6 @@ void CMemPool::Free(void* pnt, size_t numBytes)
54 55
55 56 CMemPool::~CMemPool()
56 57 {
  58 + for(std::vector<void *>::iterator i = allocated.begin(); i != allocated.end(); ++i)
  59 + ::operator delete(*i);
57 60 }
2  rts/System/MemPool.h
@@ -5,6 +5,7 @@
5 5
6 6 #include <new>
7 7 #include <cstring> // for size_t
  8 +#include <vector>
8 9
9 10 static const size_t MAX_MEM_SIZE = 200;
10 11
@@ -32,6 +33,7 @@ class CMemPool
32 33
33 34 void* nextFree[MAX_MEM_SIZE + 1];
34 35 int poolSize[MAX_MEM_SIZE + 1];
  36 + std::vector<void *> allocated;
35 37 };
36 38
37 39 extern CMemPool mempool;
7 rts/System/Platform/Misc.cpp
@@ -114,11 +114,14 @@ std::string GetOrigCWD()
114 114
115 115 void SetOrigCWD()
116 116 {
  117 + char *buf;
117 118 #ifdef WIN32
118   - origCWD = _getcwd(NULL, 0);
  119 + buf = _getcwd(NULL, 0);
119 120 #else
120   - origCWD = getcwd(NULL, 0);
  121 + buf = getcwd(NULL, 0);
121 122 #endif
  123 + origCWD = buf;
  124 + free(buf);
122 125 FileSystemAbstraction::EnsurePathSepAtEnd(origCWD);
123 126 }
124 127
11 rts/System/Platform/WindowManagerHelper.cpp
@@ -44,6 +44,17 @@ void WindowManagerHelper::SetIcon(const CBitmap* icon) {
44 44 }
45 45 }
46 46
  47 +void WindowManagerHelper::FreeIcon() {
  48 + SDL_WM_SetIcon(NULL, NULL);
  49 + if (currentIcon != NULL) {
  50 + // release the old icon
  51 + unsigned char* pixelData = (unsigned char*) currentIcon->pixels;
  52 + SDL_FreeSurface(currentIcon);
  53 + delete[] pixelData;
  54 + currentIcon = NULL;
  55 + }
  56 +}
  57 +
47 58 void WindowManagerHelper::SetCaption(const std::string& title, const std::string& titleShort) {
48 59
49 60 // titleShort may only ever be used under X11, but not QT(KDE) or Windows
1  rts/System/Platform/WindowManagerHelper.h
@@ -20,6 +20,7 @@ struct WindowManagerHelper {
20 20 * Note: Must be called before the first call to SDL_SetVideoMode.
21 21 */
22 22 static void SetIcon(const CBitmap* icon);
  23 + static void FreeIcon();
23 24
24 25 /**
25 26 * Sets the window-manager captions/titles for the running process.
8 rts/System/SpringApp.cpp
@@ -108,6 +108,7 @@ CONFIG(int, MultiThreadCount).defaultValue(0).safemodeValue(1).minimumValue(0).m
108 108 CONFIG(std::string, name).defaultValue(UnnamedPlayerName);
109 109
110 110
  111 +SelectMenu* selectMenu = NULL;
111 112 ClientSetup* startsetup = NULL;
112 113
113 114
@@ -869,7 +870,8 @@ void SpringApp::Startup()
869 870 #ifdef SYNCDEBUG
870 871 CSyncDebugger::GetInstance()->Initialize(server, 64);
871 872 #endif
872   - activeController = new SelectMenu(server);
  873 + selectMenu = new SelectMenu(server);
  874 + activeController = selectMenu;
873 875 }
874 876 else if (inputFile.rfind("sdf") == inputFile.size() - 3)
875 877 {
@@ -1046,11 +1048,14 @@ void SpringApp::Shutdown()
1046 1048 GML::Exit();
1047 1049 SafeDelete(pregame);
1048 1050 SafeDelete(game);
  1051 + agui::FreeGui();
  1052 + SafeDelete(selectMenu);
1049 1053 SafeDelete(net);
1050 1054 SafeDelete(gameServer);
1051 1055 SafeDelete(gameSetup);
1052 1056 CLoadScreen::DeleteInstance();
1053 1057 ISound::Shutdown();
  1058 + FreeJoystick();
1054 1059 SafeDelete(font);
1055 1060 SafeDelete(smallFont);
1056 1061 CNamedTextures::Kill();
@@ -1063,6 +1068,7 @@ void SpringApp::Shutdown()
1063 1068 KeyInput::FreeInstance(keyInput);
1064 1069
1065 1070 SDL_WM_GrabInput(SDL_GRAB_OFF);
  1071 + WindowManagerHelper::FreeIcon();
1066 1072 #if !defined(HEADLESS)
1067 1073 SDL_QuitSubSystem(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_JOYSTICK);
1068 1074 #endif
42 rts/aGui/Gui.cpp
@@ -21,6 +21,22 @@ Gui::Gui()
21 21
22 22 void Gui::Draw()
23 23 {
  24 + Clean();
  25 +
  26 + glDisable(GL_TEXTURE_2D);
  27 + glDisable(GL_ALPHA_TEST);
  28 + glEnable(GL_BLEND);
  29 + glMatrixMode(GL_PROJECTION);
  30 + glLoadIdentity();
  31 + gluOrtho2D(0, 1, 0, 1);
  32 + glMatrixMode(GL_MODELVIEW);
  33 + glLoadIdentity();
  34 + for (ElList::reverse_iterator it = elements.rbegin(); it != elements.rend(); ++it) {
  35 + (*it).element->Draw();
  36 + }
  37 +}
  38 +
  39 +void Gui::Clean() {
24 40 for (ElList::iterator it = toBeAdded.begin(); it != toBeAdded.end(); ++it)
25 41 {
26 42 bool duplicate = false;
@@ -56,18 +72,10 @@ void Gui::Draw()
56 72 }
57 73 }
58 74 toBeRemoved.clear();
  75 +}
59 76
60   - glDisable(GL_TEXTURE_2D);
61   - glDisable(GL_ALPHA_TEST);
62   - glEnable(GL_BLEND);
63   - glMatrixMode(GL_PROJECTION);
64   - glLoadIdentity();
65   - gluOrtho2D(0, 1, 0, 1);
66   - glMatrixMode(GL_MODELVIEW);
67   - glLoadIdentity();
68   - for (ElList::reverse_iterator it = elements.rbegin(); it != elements.rend(); ++it) {
69   - (*it).element->Draw();
70   - }
  77 +Gui::~Gui() {
  78 + Clean();
71 79 }
72 80
73 81 void Gui::AddElement(GuiElement* elem, bool asBackground)
@@ -135,8 +143,16 @@ bool Gui::HandleEvent(const SDL_Event& ev)
135 143 Gui* gui = NULL;
136 144 void InitGui()
137 145 {
138   - if (!gui)
  146 + if (gui == NULL)
139 147 gui = new Gui();
140   -};
  148 +}
  149 +
  150 +void FreeGui()
  151 +{
  152 + if (gui != NULL) {
  153 + delete gui;
  154 + gui = NULL;
  155 + }
  156 +}
141 157
142 158 }
3  rts/aGui/Gui.h
@@ -17,7 +17,9 @@ class Gui
17 17 {
18 18 public:
19 19 Gui();
  20 + virtual ~Gui();
20 21
  22 + void Clean();
21 23 void Draw();
22 24 void AddElement(GuiElement*, bool asBackground = false);
23 25 /// deletes the element on the next draw
@@ -45,6 +47,7 @@ class Gui
45 47
46 48 extern Gui* gui;
47 49 void InitGui();
  50 +void FreeGui();
48 51
49 52 }
50 53
5 rts/lib/gml/gml.cpp
@@ -353,6 +353,11 @@ ArrayBuffer(0), ElementArrayBuffer(0), PixelPackBuffer(0),PixelUnpackBuffer(0)
353 353 Size2=Queue2+GML_INIT_QUEUE_SIZE;
354 354 }
355 355
  356 +gmlQueue::~gmlQueue() {
  357 + delete Queue1;
  358 + delete Queue2;
  359 +}
  360 +
356 361 BYTE *gmlQueue::Realloc(BYTE **e) {
357 362 int oldsize=WriteSize-Write;
358 363 int newsize=oldsize*2;
1  rts/lib/gml/gmlque.h
@@ -94,6 +94,7 @@ struct gmlQueue {
94 94 GLuint PixelUnpackBuffer;
95 95
96 96 gmlQueue();
  97 + virtual ~gmlQueue();
97 98
98 99 BYTE *Realloc(BYTE **e=NULL);
99 100 BYTE *WaitRealloc(BYTE **e=NULL);

3 comments on commit d35d0b7

Johan Rehnberg

endGameBox = NULL; after delete
and lets not add unneeded ifs when we delete

zerver

Thanks, I know this, but I still want to do it because I consider it logically incorrect to delete NULL even if the system allows it.

Johan Rehnberg
Owner

Odd choice imo, having less ifs (branches) reduce code complexity and risk of bugs.
You still have the bug to fix, see my line comment above.

Please sign in to comment.
Something went wrong with that request. Please try again.