fix mem leaks and bad mem allocation

This commit is contained in:
2023-11-26 03:45:07 +02:00
parent 1f810ecedc
commit 750299b0ab
2 changed files with 16 additions and 11 deletions
+14 -11
View File
@@ -142,6 +142,8 @@ int init_fonts(const char *font_path) {
FT_Done_Face(face); FT_Done_Face(face);
FT_Done_FreeType(ft); FT_Done_FreeType(ft);
free(pixels);
return 0; return 0;
} }
@@ -341,6 +343,7 @@ void zephr_batch_text_draw() {
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
zephr_context.texts.instance_count = 0; zephr_context.texts.instance_count = 0;
memset(zephr_context.texts.instance_data, 0, sizeof(TextInstance) * zephr_context.texts.instance_count);
} }
void zephr_swap_buffers() { void zephr_swap_buffers() {
@@ -647,6 +650,17 @@ void draw_text(const char* text, int font_size, UIConstraints constraints, Color
glBindTexture(GL_TEXTURE_2D, zephr_context.font.atlas_texture_id); glBindTexture(GL_TEXTURE_2D, zephr_context.font.atlas_texture_id);
glBindVertexArray(font_vao); glBindVertexArray(font_vao);
if (zephr_context.texts.instance_count + strlen(text) >= zephr_context.texts.instance_capacity) {
zephr_context.texts.instance_capacity *= 2;
TextInstance* new_ptr = realloc(zephr_context.texts.instance_data, sizeof(TextInstance) * zephr_context.texts.instance_capacity);
if (!new_ptr) {
printf("Failed to reallocate memory for text instances\n");
exit(1);
} else {
zephr_context.texts.instance_data = new_ptr;
}
}
// we use the original text and character sizes in the loop and then we just // we use the original text and character sizes in the loop and then we just
// scale up or down the model matrix to get the desired font size. // scale up or down the model matrix to get the desired font size.
// this way everything works out fine and we get to transform the text using the // this way everything works out fine and we get to transform the text using the
@@ -669,17 +683,6 @@ void draw_text(const char* text, int font_size, UIConstraints constraints, Color
} }
zephr_context.texts.instance_count += strlen(text); zephr_context.texts.instance_count += strlen(text);
if (zephr_context.texts.instance_count >= zephr_context.texts.instance_capacity) {
zephr_context.texts.instance_capacity *= 2;
TextInstance* new_ptr = realloc(zephr_context.texts.instance_data, sizeof(TextInstance) * zephr_context.texts.instance_capacity);
if (!new_ptr) {
printf("Failed to reallocate memory for text instances\n");
exit(1);
} else {
zephr_context.texts.instance_data = new_ptr;
}
}
} }
/* enum Element { */ /* enum Element { */
+2
View File
@@ -110,6 +110,8 @@ int init_x11(Display **display, Window *window, const char* title, int window_wi
glViewport(0, 0, win_attrs.width, win_attrs.height); glViewport(0, 0, win_attrs.width, win_attrs.height);
resize_x11_window(*display, *window); resize_x11_window(*display, *window);
XFree(fbc);
return 0; return 0;
} }