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