Fixed memory leaks
This commit is contained in:
parent
5e3bf761cd
commit
de89f2bab1
@ -35,4 +35,24 @@ cmd_desc_t *get_desc(char name[])
|
|||||||
return &cmd_desc[i];
|
return &cmd_desc[i];
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void command_free(command_t *cmd)
|
||||||
|
{
|
||||||
|
if (cmd->arg_v != NULL)
|
||||||
|
free(cmd->arg_v);
|
||||||
|
|
||||||
|
free(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void command_arr_free(command_t **arr)
|
||||||
|
{
|
||||||
|
int pos = 0;
|
||||||
|
while (arr[pos] != NULL)
|
||||||
|
{
|
||||||
|
command_free(arr[pos]);
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(arr);
|
||||||
}
|
}
|
@ -41,5 +41,7 @@ typedef struct
|
|||||||
|
|
||||||
command_t *command_new(cmd_code_t type, int args[]);
|
command_t *command_new(cmd_code_t type, int args[]);
|
||||||
cmd_desc_t *get_desc(char name[]);
|
cmd_desc_t *get_desc(char name[]);
|
||||||
|
void command_free(command_t *cmd);
|
||||||
|
void command_arr_free(command_t **arr);
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -61,11 +61,16 @@ int main(int argc, char *argv[])
|
|||||||
command_t **commands = read_bin_file(argv[2], res);
|
command_t **commands = read_bin_file(argv[2], res);
|
||||||
if (commands == NULL)
|
if (commands == NULL)
|
||||||
{
|
{
|
||||||
|
stack_free(stack);
|
||||||
fprintf(stderr, "Couldn't read file\n");
|
fprintf(stderr, "Couldn't read file\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return run(commands, stack);
|
int response = run(commands, stack);
|
||||||
|
|
||||||
|
stack_free(stack);
|
||||||
|
command_arr_free(commands);
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -31,5 +31,7 @@ command_t **read_bin_file(char src[], int *res)
|
|||||||
|
|
||||||
buffer[size] = NULL;
|
buffer[size] = NULL;
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
@ -59,4 +59,10 @@ int stack_get(stack_t *stack, int pos, int *result)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return stack->mem[pos];
|
return stack->mem[pos];
|
||||||
|
}
|
||||||
|
|
||||||
|
void stack_free(stack_t *stack)
|
||||||
|
{
|
||||||
|
free(stack->mem);
|
||||||
|
free(stack);
|
||||||
}
|
}
|
@ -14,5 +14,6 @@ stack_t *stack_new(int size, int *result);
|
|||||||
int stack_push(stack_t *stack, int value, int *result);
|
int stack_push(stack_t *stack, int value, int *result);
|
||||||
int stack_pop(stack_t *stack, int *result);
|
int stack_pop(stack_t *stack, int *result);
|
||||||
int stack_get(stack_t *stack, int pos, int *result);
|
int stack_get(stack_t *stack, int pos, int *result);
|
||||||
|
void stack_free(stack_t *stack);
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
x
Reference in New Issue
Block a user