fixed roomName (folder) getting free'd before usage causing undefined behaviour

This commit is contained in:
Alexandre 2025-02-19 21:00:12 +01:00
parent a273c643c6
commit 0bb2d0da9f
9 changed files with 35 additions and 4 deletions

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -226,3 +226,13 @@ void gl_drawData(unsigned int shaderProg) {
gl_printf(shaderProg, -0.95f, 0.8f, 0.04f, 0.004f, 255, 255, 0, "coins %d", coins);
//gl_drawInteger(shaderProg, player_hp, -0.95f, 0.9f, 0.05f, 255-player_hp/4, player_hp/4, 0, 0.005f, 1);
}
void read_ppm(char* filename) {
FILE* ptr = fopen(filename, "r");
char c = fgetc(ptr);
while(c != EOF) {
printf("%c\n", c);
}
printf("\n");
fclose(ptr);
}

View File

@ -10,4 +10,6 @@ void init_vertices();
void gl_drawData(unsigned int shaderProg);
void read_ppm(char* filename);
#endif

View File

@ -224,8 +224,6 @@ void init_hashtbl() {
printf("+1\n"); fflush(stdout);
total_weight = 0;
printf("+1\n"); fflush(stdout);
to_free = malloc(sizeof(char*)*100); // ------------------------------------------------------------------------------------------------- 100 max //
to_length = 0;
}
void get_number_blocks(int* ret_cubes, int* ret_tps, int* ret_ent, FILE* ptr) {
@ -665,6 +663,11 @@ void free_pool() {
free(pool);
}
void init_to_free() {
to_free = malloc(sizeof(char*)*100); // ------------------------------------------------------------------------------------------------- 100 max //
to_length = 0;
}
void free_to_free() {
for(int k = 0; k < to_length; k++) {
free(to_free[k]);

View File

@ -56,6 +56,8 @@ void generate_nearby_chunks(int render_dist) ;
void free_pool();
void free_ent_generator();
void init_to_free();
void free_to_free();
#endif

View File

@ -70,7 +70,6 @@ void reset_everything(GLFWwindow *window, int count, char* folder) {
fflush(stdout);
free_pool();
free_to_free();
printf("-------------------------------- done 2 --------------------------------\n");
fflush(stdout);
@ -86,7 +85,20 @@ void reset_everything(GLFWwindow *window, int count, char* folder) {
printf("-------------------------------- initializing rooms... --------------------------------\n");
fflush(stdout);
parse_rooms(count, folder);
char* newFolder = malloc(sizeof(char)*52);
int i = 0;
while(folder[i] != '\0') {
newFolder[i] = folder[i];
i += 1;
}
newFolder[i] = '\0';
free_to_free();
init_to_free();
parse_rooms(count, newFolder);
free(newFolder);
printf("-------------------------------- Done 4 --------------------------------\n");
fflush(stdout);
@ -313,6 +325,7 @@ int main_alt() {
build_all_menus();
init_ent_generator(30);
init_proj();
init_to_free();
parse_rooms(7, "templates/");
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
@ -531,5 +544,6 @@ int main(int argc, char** argv) {
newRoomCount = 7;
switchRoom = false;
newRoomName = "templates/";
//read_ppm("res/smb-2.ppm");
return main_alt();
}