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();
initBullets();
initBackground();
initEffects();

View File

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

View File

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

View File

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

View File

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

View File

@ -98,7 +98,20 @@ char *getMusicFilename(int i)
void destroyResources(void)
{
free(backgrounds);
free(planets);
free(musicFiles);
int i;
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);
next = w->next;
free(w);
w = next;