Handle a malloc failure better

No memory leak
This commit is contained in:
Clownacy 2020-01-20 14:48:12 +00:00
parent 23eed95ff4
commit 767262ed8c

View file

@ -464,11 +464,7 @@ Backend_Surface* Backend_Init(SDL_Window *p_window)
program_glyph_subpixel_part1 = CompileShader(vertex_shader_texture, fragment_shader_glyph_subpixel_part1); program_glyph_subpixel_part1 = CompileShader(vertex_shader_texture, fragment_shader_glyph_subpixel_part1);
program_glyph_subpixel_part2 = CompileShader(vertex_shader_texture, fragment_shader_glyph_subpixel_part2); program_glyph_subpixel_part2 = CompileShader(vertex_shader_texture, fragment_shader_glyph_subpixel_part2);
if (program_texture == 0 || program_texture_colour_key == 0 || program_colour_fill == 0 || program_glyph_normal == 0 || program_glyph_subpixel_part1 == 0 || program_glyph_subpixel_part2 == 0) if (program_texture != 0 && program_texture_colour_key != 0 && program_colour_fill != 0 && program_glyph_normal != 0 && program_glyph_subpixel_part1 != 0 && program_glyph_subpixel_part2 != 0)
{
printf("Failed to compile shaders\n");
}
else
{ {
// Get shader uniforms // Get shader uniforms
program_colour_fill_uniform_colour = glGetUniformLocation(program_colour_fill, "colour"); program_colour_fill_uniform_colour = glGetUniformLocation(program_colour_fill, "colour");
@ -836,13 +832,14 @@ Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width
{ {
Backend_Glyph *glyph = (Backend_Glyph*)malloc(sizeof(Backend_Glyph)); Backend_Glyph *glyph = (Backend_Glyph*)malloc(sizeof(Backend_Glyph));
if (glyph == NULL) if (glyph != NULL)
return NULL; {
const unsigned int destination_pitch = (width + 3) & ~3; // Round up to the nearest 4 (OpenGL needs this) const unsigned int destination_pitch = (width + 3) & ~3; // Round up to the nearest 4 (OpenGL needs this)
unsigned char *buffer = (unsigned char*)malloc(destination_pitch * height); unsigned char *buffer = (unsigned char*)malloc(destination_pitch * height);
if (buffer != NULL)
{
switch (pixel_mode) switch (pixel_mode)
{ {
case FONT_PIXEL_MODE_LCD: case FONT_PIXEL_MODE_LCD:
@ -874,34 +871,41 @@ Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width
glGenTextures(1, &glyph->texture_id); glGenTextures(1, &glyph->texture_id);
glBindTexture(GL_TEXTURE_2D, glyph->texture_id); glBindTexture(GL_TEXTURE_2D, glyph->texture_id);
#ifdef USE_OPENGLES2 #ifdef USE_OPENGLES2
if (pixel_mode == FONT_PIXEL_MODE_LCD) if (pixel_mode == FONT_PIXEL_MODE_LCD)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width / 3, height, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width / 3, height, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer);
else else
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, buffer); glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, buffer);
#else #else
if (pixel_mode == FONT_PIXEL_MODE_LCD) if (pixel_mode == FONT_PIXEL_MODE_LCD)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width / 3, height, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width / 3, height, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer);
else else
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, buffer); glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, buffer);
#endif #endif
free(buffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
#ifndef USE_OPENGLES2 #ifndef USE_OPENGLES2
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
#endif #endif
glBindTexture(GL_TEXTURE_2D, previously_bound_texture);
glyph->width = (pixel_mode == FONT_PIXEL_MODE_LCD ? width / 3 : width); glyph->width = (pixel_mode == FONT_PIXEL_MODE_LCD ? width / 3 : width);
glyph->height = height; glyph->height = height;
glyph->pixel_mode = pixel_mode; glyph->pixel_mode = pixel_mode;
free(buffer);
glBindTexture(GL_TEXTURE_2D, previously_bound_texture);
return glyph; return glyph;
}
free(glyph);
}
return NULL;
} }
void Backend_UnloadGlyph(Backend_Glyph *glyph) void Backend_UnloadGlyph(Backend_Glyph *glyph)