@@ -18,7 +18,9 @@ use core::ffi::c_void;
1818
1919use tracing:: { instrument, Span } ;
2020use windows:: Win32 :: Foundation :: HANDLE ;
21- use windows:: Win32 :: System :: Memory :: { VirtualFreeEx , MEM_RELEASE } ;
21+ use windows:: Win32 :: System :: Memory :: {
22+ UnmapViewOfFile2 , MEMORY_MAPPED_VIEW_ADDRESS , UNMAP_VIEW_OF_FILE_FLAGS ,
23+ } ;
2224
2325use super :: surrogate_process_manager:: get_surrogate_process_manager;
2426use super :: wrappers:: HandleWrapper ;
@@ -54,10 +56,14 @@ impl Default for SurrogateProcess {
5456impl Drop for SurrogateProcess {
5557 #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
5658 fn drop ( & mut self ) {
57- let handle: HANDLE = self . process_handle . into ( ) ;
58- if let Err ( e) = unsafe { VirtualFreeEx ( handle, self . allocated_address , 0 , MEM_RELEASE ) } {
59+ let process_handle: HANDLE = self . process_handle . into ( ) ;
60+ let memory_mapped_view_address = MEMORY_MAPPED_VIEW_ADDRESS {
61+ Value : self . allocated_address ,
62+ } ;
63+ let flags = UNMAP_VIEW_OF_FILE_FLAGS ( 0 ) ;
64+ if let Err ( e) = unsafe { UnmapViewOfFile2 ( process_handle, memory_mapped_view_address, flags) } {
5965 tracing:: error!(
60- "Failed to free surrogate process resources (VirtualFreeEx failed): {:?}" ,
66+ "Failed to free surrogate process resources (UnmapViewOfFile2 failed): {:?}" ,
6167 e
6268 ) ;
6369 }
@@ -66,9 +72,21 @@ impl Drop for SurrogateProcess {
6672 // of the SurrogateProcess being dropped. this is ok to
6773 // do because we are in the process of dropping ourselves
6874 // anyway.
69- get_surrogate_process_manager ( )
70- . unwrap ( )
71- . return_surrogate_process ( self . process_handle )
72- . unwrap ( ) ;
75+ match get_surrogate_process_manager ( ) {
76+ Ok ( manager) => match manager. return_surrogate_process ( self . process_handle ) {
77+ Ok ( _) => ( ) ,
78+ Err ( e) => {
79+ tracing:: error!( "Failed to return surrogate process to surrogate process manager when dropping : {:?}" , e) ;
80+ return ;
81+ }
82+ } ,
83+ Err ( e) => {
84+ tracing:: error!(
85+ "Failed to get surrogate process manager when dropping SurrogateProcess: {:?}" ,
86+ e
87+ ) ;
88+ return ;
89+ }
90+ }
7391 }
7492}
0 commit comments