@@ -478,19 +478,35 @@ impl WebViewUriLoader {
478478 } ) = self . pop ( )
479479 {
480480 // ensure that the lock is released when the webview is destroyed before LoadEvent::Finished is handled
481- let self_c = self . clone ( ) ;
482- webview. connect_destroy ( move |_| {
483- self_c . unlock ( ) ;
484- self_c . clone ( ) . flush ( ) ;
481+ let self_ = self . clone ( ) ;
482+ let destroy_id = webview. connect_destroy ( move |_| {
483+ self_ . unlock ( ) ;
484+ self_ . clone ( ) . flush ( ) ;
485485 } ) ;
486- // we do not need to listen to failed events because those will finish the change event anyways
487- let self_c = self . clone ( ) ;
488- webview. connect_load_changed ( move |_, event| {
486+ let destroy_id_guard = Mutex :: new ( Some ( destroy_id) ) ;
487+
488+ let load_changed_id_guard = Rc :: new ( Mutex :: new ( None ) ) ;
489+ let load_changed_id_guard_ = load_changed_id_guard. clone ( ) ;
490+ let self_ = self . clone ( ) ;
491+ // noet: we do not need to listen to failed events because those will finish the change event anyways
492+ let load_changed_id = webview. connect_load_changed ( move |w, event| {
489493 if let LoadEvent :: Finished = event {
490- self_c. unlock ( ) ;
491- self_c. clone ( ) . flush ( ) ;
494+ self_. unlock ( ) ;
495+ self_. clone ( ) . flush ( ) ;
496+
497+ // unregister listeners
498+ if let Some ( id) = destroy_id_guard. lock ( ) . unwrap ( ) . take ( ) {
499+ w. disconnect ( id) ;
500+ }
501+ if let Some ( id) = load_changed_id_guard_. lock ( ) . unwrap ( ) . take ( ) {
502+ w. disconnect ( id) ;
503+ }
492504 } ;
493505 } ) ;
506+ load_changed_id_guard
507+ . lock ( )
508+ . unwrap ( )
509+ . replace ( load_changed_id) ;
494510
495511 if let Some ( headers) = headers {
496512 let req = URIRequest :: builder ( ) . uri ( & uri) . build ( ) ;
0 commit comments