Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix some mem leaks

  • Loading branch information...
commit d35d0b7abeeae7a9385a4701c460dab3c28a307b 1 parent 0946162
@zerver zerver authored
View
3  rts/Game/Game.cpp
@@ -356,6 +356,7 @@ CGame::~CGame()
teamHandler->Team(t)->Died(false);
}
+ CEndGameBox::Destroy();
CLoadScreen::DeleteInstance(); // make sure to halt loading, otherwise crash :)
// 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
gameOver = true;
eventHandler.GameOver(winningAllyTeams);
- new CEndGameBox(winningAllyTeams);
+ CEndGameBox::Create(winningAllyTeams);
#ifdef HEADLESS
profiler.PrintProfilingInfo();
#endif // HEADLESS
View
3  rts/Game/PreGame.cpp
@@ -25,6 +25,7 @@
#include "aGui/Gui.h"
#include "ExternalAI/SkirmishAIHandler.h"
+#include "Menu/SelectMenu.h"
#include "Rendering/glFont.h"
#include "Sim/Misc/GlobalSynced.h"
#include "Sim/Misc/GlobalConstants.h"
@@ -53,6 +54,7 @@ using std::string;
CONFIG(bool, DemoFromDemo).defaultValue(false);
CPreGame* pregame = NULL;
+extern SelectMenu* selectMenu;
CPreGame::CPreGame(const ClientSetup* setup) :
settings(setup),
@@ -78,6 +80,7 @@ CPreGame::~CPreGame()
{
// don't delete infoconsole, its beeing reused by CGame
agui::gui->Draw(); // delete leftover gui elements (remove once the gui is drawn ingame)
+ selectMenu = NULL;
pregame = NULL;
}
View
4 rts/Game/UI/EndGameBox.cpp
@@ -42,7 +42,7 @@ static std::string FloatToSmallString(float num, float mul = 1) {
bool CEndGameBox::enabled = true;
-
+CEndGameBox* CEndGameBox::endGameBox = NULL;
CEndGameBox::CEndGameBox(const std::vector<unsigned char>& winningAllyTeams)
: CInputReceiver()
@@ -53,6 +53,7 @@ CEndGameBox::CEndGameBox(const std::vector<unsigned char>& winningAllyTeams)
, winners(winningAllyTeams)
, graphTex(0)
{
+ endGameBox = this;
box.x1 = 0.14f;
box.y1 = 0.1f;
box.x2 = 0.86f;
@@ -88,6 +89,7 @@ CEndGameBox::~CEndGameBox()
if (graphTex) {
glDeleteTextures(1,&graphTex);
}
+ endGameBox = NULL;
}
bool CEndGameBox::MousePress(int x, int y, int button)
View
3  rts/Game/UI/EndGameBox.h
@@ -24,8 +24,11 @@ class CEndGameBox : public CInputReceiver
virtual std::string GetTooltip(int x, int y);
static bool enabled;
+ static void Create(const std::vector<unsigned char>& winningAllyTeams) { if (endGameBox == NULL) new CEndGameBox(winningAllyTeams);}
+ static void Destroy() { if (endGameBox != NULL) delete endGameBox; }
@cleanrock Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
protected:
+ static CEndGameBox* endGameBox;
void FillTeamStats();
ContainerBox box;
View
1  rts/Rendering/Env/AdvWater.cpp
@@ -347,6 +347,7 @@ void CAdvWater::UpdateWater(CGame* game)
// camera = realCam;
camera->~CCamera();
new (camera) CCamera(*(reinterpret_cast<CCamera*>(realCam)));
+ reinterpret_cast<CCamera*>(realCam)->~CCamera();
camera->Update();
glPopAttrib();
View
1  rts/Rendering/Env/BumpWater.cpp
@@ -1260,6 +1260,7 @@ void CBumpWater::DrawReflection(CGame* game)
// camera = realCam;
camera->~CCamera();
new (camera) CCamera(*(reinterpret_cast<CCamera*>(realCam)));
+ reinterpret_cast<CCamera*>(realCam)->~CCamera();
camera->Update();
}
View
1  rts/Rendering/Env/CubeMapHandler.cpp
@@ -220,6 +220,7 @@ void CubeMapHandler::CreateReflectionFace(unsigned int glType, const float3& cam
camera->~CCamera();
new (camera) CCamera(*reinterpret_cast<CCamera*>(cameraMemBuf));
+ reinterpret_cast<CCamera*>(cameraMemBuf)->~CCamera();
camera->Update();
}
View
15 rts/Rendering/Models/IModelParser.cpp
@@ -95,10 +95,14 @@ C3DModelLoader::C3DModelLoader()
C3DModelLoader::~C3DModelLoader()
{
// delete model cache
- for (std::list<S3DModel*>::iterator mi = models.begin(); mi != models.end(); ++mi) {
- S3DModel* model = *mi;
- model->DeletePieces(model->GetRootPiece());
- delete model;
+ std::set<S3DModel*> deletedModels;
+ for (ModelMap::iterator mi = cache.begin(); mi != cache.end(); ++mi) {
+ S3DModel* model = mi->second;
+ if (deletedModels.find(model) == deletedModels.end()) {
+ model->DeletePieces(model->GetRootPiece());
+ delete model;
+ deletedModels.insert(model);
+ }
}
// get rid of Spring's native parsers
@@ -240,8 +244,7 @@ S3DModel* C3DModelLoader::Load3DModel(std::string name)
cache[name] = model; // cache the model
cache[modelPath] = model; // cache the model
- models.push_back(model);
- model->id = models.size(); // IDs start with 1
+ model->id = cache.size(); // IDs start with 1
return model;
}
View
1  rts/Rendering/Models/IModelParser.h
@@ -37,7 +37,6 @@ class C3DModelLoader
private:
ModelMap cache;
ParserMap parsers;
- std::list<S3DModel*> models;
std::list<S3DModelPiece*> createLists;
View
5 rts/Sim/Misc/DefinitionTag.cpp
@@ -19,6 +19,11 @@ using std::string;
LOG_L(L_ERROR, "%s:%d: " fmt, data->GetDeclarationFile().Get().c_str(), data->GetDeclarationLine().Get(), ## __VA_ARGS__) \
+DefType::~DefType() {
+ for (MetaDataMap::const_iterator it = map.begin(); it != map.end(); ++it)
+ delete it->second;
+}
+
std::map<std::string, const DefType*>& DefType::GetTypes()
{
static std::map<std::string, const DefType*> tagtypes;
View
1  rts/Sim/Misc/DefinitionTag.h
@@ -217,6 +217,7 @@ class DefType
{
public:
DefType(const std::string& name);
+ virtual ~DefType();
template<typename T> DefTagBuilder<T> AddTag(const char* name) {
DefTagTypedMetaData<T>* meta = new DefTagTypedMetaData<T>(name);
View
6 rts/Sim/Projectiles/ProjectileHandler.cpp
@@ -10,6 +10,7 @@
#include "Map/MapInfo.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/GroundFlash.h"
+#include "Rendering/Models/S3OParser.h" // for delete [] SS3OVertex*
#include "Sim/Features/Feature.h"
#include "Sim/Features/FeatureDef.h"
#include "Sim/Misc/CollisionHandler.h"
@@ -535,8 +536,11 @@ void CProjectileHandler::AddFlyingPiece(int team, float3 pos, float3 speed, cons
void CProjectileHandler::AddFlyingPiece(int textureType, int team, float3 pos, float3 speed, SS3OVertex* verts)
{
- if (textureType <= 0)
+ if (textureType <= 0) {
+ if (verts != NULL)
+ delete [] verts;
return; // texture 0 means 3do
+ }
FlyingPiece* fp = new FlyingPiece(team, pos, speed, textureType, verts);
flyingPiecesS3O.insert(fp);
View
22 rts/Sim/Units/Scripts/CobEngine.cpp
@@ -37,18 +37,26 @@ CCobEngine::CCobEngine()
CCobEngine::~CCobEngine()
{
//Should delete all things that the scheduler knows
- for (std::list<CCobThread *>::iterator i = running.begin(); i != running.end(); ++i) {
- delete *i;
+ while (!running.empty()) {
+ CCobThread *tmp = running.front();
+ tmp->SetCallback(NULL, NULL, NULL);
+ running.pop_front();
+ delete tmp;
}
- for (std::list<CCobThread *>::iterator i = wantToRun.begin(); i != wantToRun.end(); ++i) {
- delete *i;
+ while(!wantToRun.empty()) {
+ CCobThread *tmp = wantToRun.front();
+ tmp->SetCallback(NULL, NULL, NULL);
+ wantToRun.pop_front();
+ delete tmp;
}
- while (sleeping.size() > 0) {
- CCobThread *tmp;
- tmp = sleeping.top();
+ while (!sleeping.empty()) {
+ CCobThread *tmp = sleeping.top();
+ tmp->SetCallback(NULL, NULL, NULL);
sleeping.pop();
delete tmp;
}
+
+ assert(running.empty() && wantToRun.empty() && sleeping.empty());
}
View
7 rts/System/Input/Joystick.cpp
@@ -33,6 +33,13 @@ void InitJoystick()
};
}
+void FreeJoystick() {
+ if (stick != NULL) {
+ delete stick;
+ stick = NULL;
+ }
+}
+
Joystick::Joystick()
{
const int numSticks = SDL_NumJoysticks();
View
1  rts/System/Input/Joystick.h
@@ -9,6 +9,7 @@
union SDL_Event;
void InitJoystick();
+void FreeJoystick();
class Joystick
{
View
3  rts/System/MemPool.cpp
@@ -24,6 +24,7 @@ void* CMemPool::Alloc(size_t numBytes)
nextFree[numBytes] = (*(void**)pnt);
} else {
void* newBlock = ::operator new(numBytes * poolSize[numBytes]);
+ allocated.push_back(newBlock);
for (int i = 0; i < (poolSize[numBytes] - 1); ++i) {
*(void**)&((char*)newBlock)[(i) * numBytes] = (void*)&((char*)newBlock)[(i + 1) * numBytes];
}
@@ -54,4 +55,6 @@ void CMemPool::Free(void* pnt, size_t numBytes)
CMemPool::~CMemPool()
{
+ for(std::vector<void *>::iterator i = allocated.begin(); i != allocated.end(); ++i)
+ ::operator delete(*i);
}
View
2  rts/System/MemPool.h
@@ -5,6 +5,7 @@
#include <new>
#include <cstring> // for size_t
+#include <vector>
static const size_t MAX_MEM_SIZE = 200;
@@ -32,6 +33,7 @@ class CMemPool
void* nextFree[MAX_MEM_SIZE + 1];
int poolSize[MAX_MEM_SIZE + 1];
+ std::vector<void *> allocated;
};
extern CMemPool mempool;
View
7 rts/System/Platform/Misc.cpp
@@ -114,11 +114,14 @@ std::string GetOrigCWD()
void SetOrigCWD()
{
+ char *buf;
#ifdef WIN32
- origCWD = _getcwd(NULL, 0);
+ buf = _getcwd(NULL, 0);
#else
- origCWD = getcwd(NULL, 0);
+ buf = getcwd(NULL, 0);
#endif
+ origCWD = buf;
+ free(buf);
FileSystemAbstraction::EnsurePathSepAtEnd(origCWD);
}
View
11 rts/System/Platform/WindowManagerHelper.cpp
@@ -44,6 +44,17 @@ void WindowManagerHelper::SetIcon(const CBitmap* icon) {
}
}
+void WindowManagerHelper::FreeIcon() {
+ SDL_WM_SetIcon(NULL, NULL);
+ if (currentIcon != NULL) {
+ // release the old icon
+ unsigned char* pixelData = (unsigned char*) currentIcon->pixels;
+ SDL_FreeSurface(currentIcon);
+ delete[] pixelData;
+ currentIcon = NULL;
+ }
+}
+
void WindowManagerHelper::SetCaption(const std::string& title, const std::string& titleShort) {
// titleShort may only ever be used under X11, but not QT(KDE) or Windows
View
1  rts/System/Platform/WindowManagerHelper.h
@@ -20,6 +20,7 @@ struct WindowManagerHelper {
* Note: Must be called before the first call to SDL_SetVideoMode.
*/
static void SetIcon(const CBitmap* icon);
+ static void FreeIcon();
/**
* Sets the window-manager captions/titles for the running process.
View
8 rts/System/SpringApp.cpp
@@ -108,6 +108,7 @@ CONFIG(int, MultiThreadCount).defaultValue(0).safemodeValue(1).minimumValue(0).m
CONFIG(std::string, name).defaultValue(UnnamedPlayerName);
+SelectMenu* selectMenu = NULL;
ClientSetup* startsetup = NULL;
@@ -869,7 +870,8 @@ void SpringApp::Startup()
#ifdef SYNCDEBUG
CSyncDebugger::GetInstance()->Initialize(server, 64);
#endif
- activeController = new SelectMenu(server);
+ selectMenu = new SelectMenu(server);
+ activeController = selectMenu;
}
else if (inputFile.rfind("sdf") == inputFile.size() - 3)
{
@@ -1046,11 +1048,14 @@ void SpringApp::Shutdown()
GML::Exit();
SafeDelete(pregame);
SafeDelete(game);
+ agui::FreeGui();
+ SafeDelete(selectMenu);
SafeDelete(net);
SafeDelete(gameServer);
SafeDelete(gameSetup);
CLoadScreen::DeleteInstance();
ISound::Shutdown();
+ FreeJoystick();
SafeDelete(font);
SafeDelete(smallFont);
CNamedTextures::Kill();
@@ -1063,6 +1068,7 @@ void SpringApp::Shutdown()
KeyInput::FreeInstance(keyInput);
SDL_WM_GrabInput(SDL_GRAB_OFF);
+ WindowManagerHelper::FreeIcon();
#if !defined(HEADLESS)
SDL_QuitSubSystem(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_JOYSTICK);
#endif
View
42 rts/aGui/Gui.cpp
@@ -21,6 +21,22 @@ Gui::Gui()
void Gui::Draw()
{
+ Clean();
+
+ glDisable(GL_TEXTURE_2D);
+ glDisable(GL_ALPHA_TEST);
+ glEnable(GL_BLEND);
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ gluOrtho2D(0, 1, 0, 1);
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+ for (ElList::reverse_iterator it = elements.rbegin(); it != elements.rend(); ++it) {
+ (*it).element->Draw();
+ }
+}
+
+void Gui::Clean() {
for (ElList::iterator it = toBeAdded.begin(); it != toBeAdded.end(); ++it)
{
bool duplicate = false;
@@ -56,18 +72,10 @@ void Gui::Draw()
}
}
toBeRemoved.clear();
+}
- glDisable(GL_TEXTURE_2D);
- glDisable(GL_ALPHA_TEST);
- glEnable(GL_BLEND);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0, 1, 0, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- for (ElList::reverse_iterator it = elements.rbegin(); it != elements.rend(); ++it) {
- (*it).element->Draw();
- }
+Gui::~Gui() {
+ Clean();
}
void Gui::AddElement(GuiElement* elem, bool asBackground)
@@ -135,8 +143,16 @@ bool Gui::HandleEvent(const SDL_Event& ev)
Gui* gui = NULL;
void InitGui()
{
- if (!gui)
+ if (gui == NULL)
gui = new Gui();
-};
+}
+
+void FreeGui()
+{
+ if (gui != NULL) {
+ delete gui;
+ gui = NULL;
+ }
+}
}
View
3  rts/aGui/Gui.h
@@ -17,7 +17,9 @@ class Gui
{
public:
Gui();
+ virtual ~Gui();
+ void Clean();
void Draw();
void AddElement(GuiElement*, bool asBackground = false);
/// deletes the element on the next draw
@@ -45,6 +47,7 @@ class Gui
extern Gui* gui;
void InitGui();
+void FreeGui();
}
View
5 rts/lib/gml/gml.cpp
@@ -353,6 +353,11 @@ ArrayBuffer(0), ElementArrayBuffer(0), PixelPackBuffer(0),PixelUnpackBuffer(0)
Size2=Queue2+GML_INIT_QUEUE_SIZE;
}
+gmlQueue::~gmlQueue() {
+ delete Queue1;
+ delete Queue2;
+}
+
BYTE *gmlQueue::Realloc(BYTE **e) {
int oldsize=WriteSize-Write;
int newsize=oldsize*2;
View
1  rts/lib/gml/gmlque.h
@@ -94,6 +94,7 @@ struct gmlQueue {
GLuint PixelUnpackBuffer;
gmlQueue();
+ virtual ~gmlQueue();
BYTE *Realloc(BYTE **e=NULL);
BYTE *WaitRealloc(BYTE **e=NULL);

3 comments on commit d35d0b7

@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.

@cleanrock
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.