@@ -420,17 +420,24 @@ mod windows {
420420 let border_x = util:: get_system_metrics_for_dpi ( SM_CXFRAME , dpi) ;
421421 let border_y = util:: get_system_metrics_for_dpi ( SM_CYFRAME , dpi) ;
422422
423- let hrgn1 = CreateRectRgn ( 0 , 0 , width, height) ;
423+ // hrgn1 must be mutable to call .free() later
424+ let mut hrgn1 = CreateRectRgn ( 0 , 0 , width, height) ;
424425
425426 let x1 = if only_top { 0 } else { border_x } ;
426427 let y1 = border_y;
427428 let x2 = if only_top { width } else { width - border_x } ;
428429 let y2 = if only_top { height } else { height - border_y } ;
429- let hrgn2 = CreateRectRgn ( x1, y1, x2, y2) ;
430430
431- CombineRgn ( Some ( hrgn1) , Some ( hrgn1) , Some ( hrgn2) , RGN_DIFF ) ;
431+ // Wrap hrgn2 in Owned so it is automatically freed when going out of scope
432+ let hrgn2 = Owned :: new ( CreateRectRgn ( x1, y1, x2, y2) ) ;
432433
433- SetWindowRgn ( hwnd, Some ( hrgn1) , true ) ;
434+ CombineRgn ( Some ( hrgn1) , Some ( hrgn1) , Some ( * hrgn2) , RGN_DIFF ) ;
435+
436+ // Try to set the window region
437+ if SetWindowRgn ( hwnd, Some ( hrgn1) , true ) == 0 {
438+ // If it fails, we must free hrgn1 manually
439+ hrgn1. free ( ) ;
440+ }
434441 }
435442
436443 pub fn update_drag_hwnd_rgn_for_undecorated ( hwnd : isize , has_undecorated_shadows : bool ) {
0 commit comments