Commit 4afeeea5 authored by ewen.madec's avatar ewen.madec

fix memory leak

parent 2d0a1477
...@@ -161,16 +161,18 @@ int main(int nbArg, char ** arg) ...@@ -161,16 +161,18 @@ int main(int nbArg, char ** arg)
// proper closing // proper closing
//--------------- //---------------
NetWorld_delete(world);
Players_delete(players, playerCount); Players_delete(players, playerCount);
Mission_delete(missions); Mission_deleteArray(missions,playerCount);
NetWorld_delete(world);
Main_Menu_delete(menu); Main_Menu_delete(menu);
Main_Menu_delete(rulesMenu); Main_Menu_delete(rulesMenu);
Menu_Button_delete(gameReturn);
Options_Menu_delete(optionsMenu); Options_Menu_delete(optionsMenu);
Window_Manager_delete(manager); Window_Manager_delete(manager);
Panel_delete(panel); Panel_delete(panel);
UnloadFont(gameFont); UnloadFont(gameFont);
CloseWindow(); // Close window and OpenGL context CloseWindow(); // Close window and OpenGL context
return 0; return 0;
......
...@@ -11,8 +11,7 @@ bool Player_eliminated(NetWorld * world, Player * players, const int playerID, c ...@@ -11,8 +11,7 @@ bool Player_eliminated(NetWorld * world, Player * players, const int playerID, c
} }
Mission * Mission_init(const int playerID, NetWorld * world){ void Mission_init(Mission * self, const int playerID, NetWorld * world){
Mission * self = malloc(sizeof(Mission));
char* displayText = malloc(sizeof(char)*50); char* displayText = malloc(sizeof(char)*50);
self->playerID = playerID; self->playerID = playerID;
...@@ -34,13 +33,13 @@ Mission * Mission_init(const int playerID, NetWorld * world){ ...@@ -34,13 +33,13 @@ Mission * Mission_init(const int playerID, NetWorld * world){
self->displayText = displayText; self->displayText = displayText;
break; break;
} }
return self; //return self;
} }
Mission * Mission_newArray(const int playerCount, NetWorld * world){ Mission * Mission_newArray(const int playerCount, NetWorld * world){
Mission * missionArray = malloc(sizeof(Mission) * playerCount); Mission * missionArray = malloc(sizeof(Mission) * playerCount);
for(int i = 0; i < playerCount; i++){ for(int i = 0; i < playerCount; i++){
missionArray[i] = *Mission_init(i, world); Mission_init((missionArray+i),i ,world);
} }
return missionArray; return missionArray;
} }
...@@ -51,6 +50,9 @@ bool Mission_check(Mission * self, NetWorld * world, Player * players){ ...@@ -51,6 +50,9 @@ bool Mission_check(Mission * self, NetWorld * world, Player * players){
return self->mission(world, players, self->playerID, self->target); return self->mission(world, players, self->playerID, self->target);
} }
void Mission_delete(Mission * self){ void Mission_deleteArray(Mission * array, int playerCount){
free(self); for(int i = 0 ; i<playerCount; i++){
free((array+i)->displayText);
}
free(array);
} }
\ No newline at end of file
...@@ -13,11 +13,11 @@ struct Str_Mission{ ...@@ -13,11 +13,11 @@ struct Str_Mission{
typedef struct Str_Mission Mission; typedef struct Str_Mission Mission;
Mission * Mission_init(const int playerID, NetWorld * world); void Mission_init(Mission * self, const int playerID, NetWorld * world);
bool Mission_check(Mission * self, NetWorld * world, Player * players); bool Mission_check(Mission * self, NetWorld * world, Player * players);
void Mission_delete(Mission * self); void Mission_deleteArray(Mission * array, int playerCount);
Mission * Mission_newArray(const int playerCount, NetWorld * world); Mission * Mission_newArray(const int playerCount, NetWorld * world);
......
...@@ -12,18 +12,14 @@ ...@@ -12,18 +12,14 @@
// Constructor / Destructor // Constructor / Destructor
void Node_construct(Node * self) void Node_construct(Node * self)
{ {
Node_set( self, (Vector2){0.f, 0.f}, GRAY, "" ); Node_set( self, (Vector2){0.f, 0.f}, GRAY);
self->card= 0; self->card= 0;
self->edges= Edge_newArray( 0 ); self->edges= Edge_newArray( 0 );
self->name= malloc( sizeof(char)*32 );
self->continent= malloc( sizeof(char)*32 );
self->soldiers = 1; self->soldiers = 1;
self->soldiersToAdd = 0; self->soldiersToAdd = 0;
self->playerID = -1; self->playerID = -1;
Rectangle tmp = {0,0,48,48}; Rectangle tmp = {0,0,48,48};
self->collisionHitbox = tmp; self->collisionHitbox = tmp;
self->continent = "None";
self->name = "Node" ;
self->canAttack = true; self->canAttack = true;
} }
...@@ -47,10 +43,7 @@ Node * Node_newArray(int size) ...@@ -47,10 +43,7 @@ Node * Node_newArray(int size)
void Node_delete( Node * node ) void Node_delete( Node * node )
{ {
free(node->continent);
free(node->name);
Edge_deleteArray( node->card, node->edges ); Edge_deleteArray( node->card, node->edges );
free( node );
} }
/** /**
...@@ -58,9 +51,9 @@ void Node_delete( Node * node ) ...@@ -58,9 +51,9 @@ void Node_delete( Node * node )
*/ */
void Node_deleteArray( int n, Node * array ) void Node_deleteArray( int n, Node * array )
{ {
for(int i= 0 ; i < n ; ++i ) for(int i= 0 ; i < n ; ++i ){
Node_delete((array+i)); Node_delete((array+i));
//Edge_deleteArray( array[i].card, array[i].edges ); }
free( array ); free( array );
} }
...@@ -93,11 +86,10 @@ void Node_resize( Node * self, int card ) ...@@ -93,11 +86,10 @@ void Node_resize( Node * self, int card )
self->card= card; self->card= card;
} }
void Node_set( Node * self, Vector2 position, Color color, char * name ) void Node_set( Node * self, Vector2 position, Color color)
{ {
self->position= position; self->position= position;
self->color= color; self->color= color;
self->name = name;
} }
int Node_connect( Node * self, Node * target ) int Node_connect( Node * self, Node * target )
......
...@@ -18,14 +18,10 @@ struct Str_Node { ...@@ -18,14 +18,10 @@ struct Str_Node {
//! cardinality of the node (i.e. number of edges) //! cardinality of the node (i.e. number of edges)
int card; int card;
// Content: // Content:
//! name of the node
char* name;
//! Number of soldiers at the node //! Number of soldiers at the node
int soldiers; int soldiers;
//! Buffer variable to stop players from removing soldiers from their nodes //! Buffer variable to stop players from removing soldiers from their nodes
int soldiersToAdd; int soldiersToAdd;
//! Continent the node belongs to
char* continent;
//! Player the node belongs to //! Player the node belongs to
int playerID; int playerID;
//! Click hitbox for the node //! Click hitbox for the node
...@@ -110,10 +106,7 @@ void Node_set( ...@@ -110,10 +106,7 @@ void Node_set(
//! position //! position
Vector2 position, Vector2 position,
//! color //! color
Color color, Color color
//!
char * name
); );
/** /**
......
...@@ -36,8 +36,8 @@ Player * Player_newArray(int size, Color color[]) ...@@ -36,8 +36,8 @@ Player * Player_newArray(int size, Color color[])
void Players_delete(Player * player, int size) void Players_delete(Player * player, int size)
{ {
for(int i = 0 ; i < size ; i++){ for(int i = 0 ; i < size ; i++){
free((player+i)->nodes); //free((player+i)->nodes);
free((player+i)->selectedNode); //free((player+i)->selectedNode);
} }
free(player); free(player);
} }
...@@ -56,6 +56,7 @@ void Player_add_Node( Player * self, Node * node ) ...@@ -56,6 +56,7 @@ void Player_add_Node( Player * self, Node * node )
node->playerID = self->ID; node->playerID = self->ID;
newNodes[self->nodeCount] = node; newNodes[self->nodeCount] = node;
free(self->nodes); free(self->nodes);
self->nodes = NULL;
self->nodes= newNodes; self->nodes= newNodes;
self->nodeCount += 1; self->nodeCount += 1;
} }
...@@ -161,9 +162,6 @@ int * Player_attack_Node(Player * self, Player * defender, Node * originNode, No ...@@ -161,9 +162,6 @@ int * Player_attack_Node(Player * self, Player * defender, Node * originNode, No
int attDiceRolls[attackersCount]; int attDiceRolls[attackersCount];
int defDiceRolls[defendersCount]; int defDiceRolls[defendersCount];
printf("%i attackers\n", attackersCount);
printf("%i defenders\n", defendersCount);
for(int index = 0; index < attackersCount; index++){ for(int index = 0; index < attackersCount; index++){
attDiceRolls[index] = (rand() % 6) + 1; attDiceRolls[index] = (rand() % 6) + 1;
} }
...@@ -230,9 +228,6 @@ int * Player_attack_Node(Player * self, Player * defender, Node * originNode, No ...@@ -230,9 +228,6 @@ int * Player_attack_Node(Player * self, Player * defender, Node * originNode, No
} }
originNode->canAttack = false; originNode->canAttack = false;
for(int i = 0; i< defendersCount+attackersCount; i++){
printf("listOfdices2 [%d] : %d \n",i,listOfDices[i]);
}
return listOfDices; return listOfDices;
} }
......
...@@ -63,17 +63,16 @@ void Random_map(NetWorld *world) ...@@ -63,17 +63,16 @@ void Random_map(NetWorld *world)
} }
} }
} }
char *name; name = malloc(20*sizeof(char)); sprintf(name, "name : %d", i); //har *name; name = malloc(20*sizeof(char)); sprintf(name, "name : %d", i);
Node_set(&(world->nodes[i]), (Vector2){randomX, randomY}, RED, name); Node_set(&(world->nodes[i]), (Vector2){randomX, randomY}, RED);
free(name); //free(name);
} }
else else
{ {
randomX = randfrom(-15.0, 15.0); randomX = randfrom(-15.0, 15.0);
randomY = randfrom(-15.0, 15.0); randomY = randfrom(-15.0, 15.0);
char *name; name = malloc(20*sizeof(char)); sprintf(name, "name : %d", i); //char name[20]; sprintf(name, "name : %d", i);
Node_set(&(world->nodes[i]), (Vector2){randomX, randomY}, RED, name); Node_set(&(world->nodes[i]), (Vector2){randomX, randomY}, RED);
free(name);
} }
} }
//Gabriel graph sort //Gabriel graph sort
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment