@@ -18,7 +18,9 @@ use core::ffi::c_void;
18
18
19
19
use tracing:: { instrument, Span } ;
20
20
use 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
+ } ;
22
24
23
25
use super :: surrogate_process_manager:: get_surrogate_process_manager;
24
26
use super :: wrappers:: HandleWrapper ;
@@ -54,10 +56,14 @@ impl Default for SurrogateProcess {
54
56
impl Drop for SurrogateProcess {
55
57
#[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
56
58
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) } {
59
65
tracing:: error!(
60
- "Failed to free surrogate process resources (VirtualFreeEx failed): {:?}" ,
66
+ "Failed to free surrogate process resources (UnmapViewOfFile2 failed): {:?}" ,
61
67
e
62
68
) ;
63
69
}
@@ -66,9 +72,21 @@ impl Drop for SurrogateProcess {
66
72
// of the SurrogateProcess being dropped. this is ok to
67
73
// do because we are in the process of dropping ourselves
68
74
// 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
+ }
73
91
}
74
92
}
0 commit comments