Memory leak fixes.

This commit is contained in:
Steve 2016-03-10 22:47:33 +00:00
parent ed438db279
commit 7ba4dcf26c
7 changed files with 35 additions and 21 deletions

View File

@ -60,8 +60,6 @@ void initBattle(void)
initStars(); initStars();
initBullets();
initBackground(); initBackground();
initEffects(); initEffects();

View File

@ -54,6 +54,7 @@ void loadItemDefs(void)
SDL_QueryTexture(e->texture, NULL, NULL, &e->w, &e->h); SDL_QueryTexture(e->texture, NULL, NULL, &e->w, &e->h);
defTail->next = e; defTail->next = e;
defTail = e;
} }
cJSON_Delete(root); cJSON_Delete(root);

View File

@ -24,8 +24,6 @@ static Entity **candidates;
static int cIndex; static int cIndex;
static int cCapacity; static int cCapacity;
static int memory;
static int getIndex(Quadtree *root, int x, int y, int w, int h); static int getIndex(Quadtree *root, int x, int y, int w, int h);
static void removeEntity(Entity *e, Quadtree *root); static void removeEntity(Entity *e, Quadtree *root);
static int candidatesComparator(const void *a, const void *b); static int candidatesComparator(const void *a, const void *b);
@ -48,11 +46,12 @@ void initQuadtree(Quadtree *root)
root->ents = malloc(sizeof(Entity*) * root->capacity); root->ents = malloc(sizeof(Entity*) * root->capacity);
memset(root->ents, 0, sizeof(Entity*) * root->capacity); memset(root->ents, 0, sizeof(Entity*) * root->capacity);
memory = 0; cIndex = 0;
cCapacity = QT_INITIAL_CAPACITY;
candidates = malloc(sizeof(Entity*) * cCapacity);
memset(candidates, 0, sizeof(Entity*) * cCapacity);
} }
memory += sizeof(Quadtree);
w = root->w / 2; w = root->w / 2;
h = root->h / 2; h = root->h / 2;
@ -101,11 +100,6 @@ void initQuadtree(Quadtree *root)
initQuadtree(node); initQuadtree(node);
} }
} }
cIndex = 0;
cCapacity = QT_INITIAL_CAPACITY;
candidates = malloc(sizeof(Entity*) * cCapacity);
memset(candidates, 0, sizeof(Entity*) * cCapacity);
} }
void addToQuadtree(Entity *e, Quadtree *root) void addToQuadtree(Entity *e, Quadtree *root)
@ -277,23 +271,28 @@ void destroyQuadtree(void)
{ {
destroyQuadtreeNode(&battle.quadtree); destroyQuadtreeNode(&battle.quadtree);
free(candidates); if (candidates)
{
free(candidates);
candidates = NULL; candidates = NULL;
}
} }
static void destroyQuadtreeNode(Quadtree *root) static void destroyQuadtreeNode(Quadtree *root)
{ {
int i; int i;
free(root->ents);
root->ents = NULL;
if (root->node[0]) if (root->node[0])
{ {
for (i = 0 ; i < 4 ; i++) for (i = 0 ; i < 4 ; i++)
{ {
destroyQuadtreeNode(root->node[i]); destroyQuadtreeNode(root->node[i]);
free(root->node[i]->ents);
free(root->node[i]); free(root->node[i]);
root->node[i] = NULL; root->node[i] = NULL;

View File

@ -198,6 +198,8 @@ void loadMission(char *filename)
srand(time(NULL)); srand(time(NULL));
cJSON_Delete(root);
free(text); free(text);
endSectionTransition(); endSectionTransition();

View File

@ -131,7 +131,6 @@ void initGameSystem(void)
initStarSystems, initStarSystems,
initChallenges, initChallenges,
initStats, initStats,
initBattle,
initModalDialog, initModalDialog,
initBackground, initBackground,
initControls initControls

View File

@ -98,7 +98,20 @@ char *getMusicFilename(int i)
void destroyResources(void) void destroyResources(void)
{ {
free(backgrounds); int i;
free(planets);
free(musicFiles); for (i = 0 ; i < numBackgrounds ; i++)
{
free(backgrounds[i]);
}
for (i = 0 ; i < numPlanets ; i++)
{
free(planets[i]);
}
for (i = 0 ; i < numMusicFiles ; i++)
{
free(musicFiles[i]);
}
} }

View File

@ -508,6 +508,8 @@ void destroyWidgets(void)
free(w->options[i]); free(w->options[i]);
} }
free(w->options);
next = w->next; next = w->next;
free(w); free(w);
w = next; w = next;