From 07bfc119b8fe3f39b1da03f316791f15ec7bad1e Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Thu, 16 Jul 2009 07:33:14 +0000 Subject: [PATCH] -No need to check if a pointer is null before freeing it. -However if the pointer is still living (for example a global) you should assign NULL to it to avoid bad ram access and random bugs. Accessing a NULL pointer always gives a segfault, accessing a freed pointer may or may not crash. -Also fixed a memory leak git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@924 416bcca6-2ee7-4201-b75f-2eb2f807beb1 --- buttons.c | 25 +++++++++++-------------- graph.c | 3 +-- loadsave.c | 4 ++-- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/buttons.c b/buttons.c index 59671543..36ea052d 100644 --- a/buttons.c +++ b/buttons.c @@ -2735,6 +2735,8 @@ void Load_picture(byte image) use_brush_palette=Confirmation_box("Use the palette of the brush?"); } + // do_not_restore is modified inside the first if, that's why we check it + // again here if (do_not_restore) { old_cursor_shape=Cursor_shape; @@ -2764,13 +2766,13 @@ void Load_picture(byte image) if (File_error==3) // On ne peut pas allouer la brosse { - if (Brush) free(Brush); + free(Brush); Brush=(byte *)malloc(1*1); Brush_height=1; Brush_width=1; *Brush=Fore_color; - if (Smear_brush) free(Smear_brush); + free(Smear_brush); Smear_brush=(byte *)malloc(MAX_PAINTBRUSH_SIZE*MAX_PAINTBRUSH_SIZE); Smear_brush_height=MAX_PAINTBRUSH_SIZE; Smear_brush_width=MAX_PAINTBRUSH_SIZE; @@ -2828,7 +2830,7 @@ void Load_picture(byte image) } new_mode=Best_video_mode(); - // TODO : Utiliser içi Ratio_of_loaded_image pour passer dans la + // TODO : Utiliser ici Ratio_of_loaded_image pour passer dans la // bonne taille de pixels. if ((Config.Auto_set_res) && (new_mode!=Current_resolution)) { @@ -2858,8 +2860,7 @@ void Load_picture(byte image) Display_cursor(); } - if (!image) - free(initial_palette); + free(initial_palette); if (!do_not_restore) { @@ -4863,10 +4864,9 @@ void Button_Sieve_menu(void) break; case 7 : // Transfer to brush - if (Brush) - free(Brush); Brush_width=Sieve_width; Brush_height=Sieve_height; + free(Brush); Brush=(byte *)malloc(((long)Brush_height)*Brush_width); for (y_pos=0; y_posList_start; cursor_position = font_list->Cursor_position; - if (new_brush) - free(new_brush); + free(new_brush); + new_brush = NULL; Close_window(); Unselect_button(BUTTON_TEXT); Display_cursor(); diff --git a/graph.c b/graph.c index b01133ad..e97cb30a 100644 --- a/graph.c +++ b/graph.c @@ -495,8 +495,7 @@ int Init_mode_video(int width, int height, int fullscreen, int pix_ratio) else if (Pixel_width>Pixel_height && Screen_height>=Menu_factor_Y*2*200) Menu_factor_Y*=2; - if (Horizontal_line_buffer) - free(Horizontal_line_buffer); + free(Horizontal_line_buffer); Horizontal_line_buffer=(byte *)malloc(Pixel_width*((Screen_width>Main_image_width)?Screen_width:Main_image_width)); Set_palette(Main_palette); diff --git a/loadsave.c b/loadsave.c index 9a894320..5aeecbbc 100644 --- a/loadsave.c +++ b/loadsave.c @@ -5483,8 +5483,8 @@ void Load_PC1(void) else { File_error=1; - if (bufferdecomp) free(bufferdecomp); - if (buffercomp) free(buffercomp); + free(bufferdecomp); + free(buffercomp); } fclose(file); }