-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
This commit is contained in:
Adrien Destugues 2009-07-16 07:33:14 +00:00
parent 8b5681eccb
commit 07bfc119b8
3 changed files with 14 additions and 18 deletions

View File

@ -2735,6 +2735,8 @@ void Load_picture(byte image)
use_brush_palette=Confirmation_box("Use the palette of the brush?"); 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) if (do_not_restore)
{ {
old_cursor_shape=Cursor_shape; 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 (File_error==3) // On ne peut pas allouer la brosse
{ {
if (Brush) free(Brush); free(Brush);
Brush=(byte *)malloc(1*1); Brush=(byte *)malloc(1*1);
Brush_height=1; Brush_height=1;
Brush_width=1; Brush_width=1;
*Brush=Fore_color; *Brush=Fore_color;
if (Smear_brush) free(Smear_brush); free(Smear_brush);
Smear_brush=(byte *)malloc(MAX_PAINTBRUSH_SIZE*MAX_PAINTBRUSH_SIZE); Smear_brush=(byte *)malloc(MAX_PAINTBRUSH_SIZE*MAX_PAINTBRUSH_SIZE);
Smear_brush_height=MAX_PAINTBRUSH_SIZE; Smear_brush_height=MAX_PAINTBRUSH_SIZE;
Smear_brush_width=MAX_PAINTBRUSH_SIZE; Smear_brush_width=MAX_PAINTBRUSH_SIZE;
@ -2828,7 +2830,7 @@ void Load_picture(byte image)
} }
new_mode=Best_video_mode(); 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. // bonne taille de pixels.
if ((Config.Auto_set_res) && (new_mode!=Current_resolution)) if ((Config.Auto_set_res) && (new_mode!=Current_resolution))
{ {
@ -2858,8 +2860,7 @@ void Load_picture(byte image)
Display_cursor(); Display_cursor();
} }
if (!image) free(initial_palette);
free(initial_palette);
if (!do_not_restore) if (!do_not_restore)
{ {
@ -4863,10 +4864,9 @@ void Button_Sieve_menu(void)
break; break;
case 7 : // Transfer to brush case 7 : // Transfer to brush
if (Brush)
free(Brush);
Brush_width=Sieve_width; Brush_width=Sieve_width;
Brush_height=Sieve_height; Brush_height=Sieve_height;
free(Brush);
Brush=(byte *)malloc(((long)Brush_height)*Brush_width); Brush=(byte *)malloc(((long)Brush_height)*Brush_width);
for (y_pos=0; y_pos<Sieve_height; y_pos++) for (y_pos=0; y_pos<Sieve_height; y_pos++)
for (x_pos=0; x_pos<Sieve_width; x_pos++) for (x_pos=0; x_pos<Sieve_width; x_pos++)
@ -5505,11 +5505,8 @@ void Button_Text()
const char * preview_string = "AaBbCcDdEeFf012345"; const char * preview_string = "AaBbCcDdEeFf012345";
if (str[0]) if (str[0])
preview_string=str; preview_string=str;
if (new_brush)
{
free(new_brush);
}
Window_rectangle(8, 106, 273, 50,Back_color); Window_rectangle(8, 106, 273, 50,Back_color);
free(new_brush);
new_brush = Render_text(preview_string, selected_font_index, font_size, antialias, is_bold, is_italic, &new_width, &new_height); new_brush = Render_text(preview_string, selected_font_index, font_size, antialias, is_bold, is_italic, &new_width, &new_height);
if (new_brush) if (new_brush)
{ {
@ -5636,7 +5633,7 @@ void Button_Text()
Error(0); Error(0);
return; return;
} }
if (Brush) free(Brush); free(Brush);
Brush=new_brush; Brush=new_brush;
Brush_width=new_width; Brush_width=new_width;
@ -5670,8 +5667,8 @@ void Button_Text()
list_start = font_list->List_start; list_start = font_list->List_start;
cursor_position = font_list->Cursor_position; cursor_position = font_list->Cursor_position;
if (new_brush) free(new_brush);
free(new_brush); new_brush = NULL;
Close_window(); Close_window();
Unselect_button(BUTTON_TEXT); Unselect_button(BUTTON_TEXT);
Display_cursor(); Display_cursor();

View File

@ -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) else if (Pixel_width>Pixel_height && Screen_height>=Menu_factor_Y*2*200)
Menu_factor_Y*=2; 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)); Horizontal_line_buffer=(byte *)malloc(Pixel_width*((Screen_width>Main_image_width)?Screen_width:Main_image_width));
Set_palette(Main_palette); Set_palette(Main_palette);

View File

@ -5483,8 +5483,8 @@ void Load_PC1(void)
else else
{ {
File_error=1; File_error=1;
if (bufferdecomp) free(bufferdecomp); free(bufferdecomp);
if (buffercomp) free(buffercomp); free(buffercomp);
} }
fclose(file); fclose(file);
} }