@@ -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,16 @@ 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) =
65
+ unsafe { UnmapViewOfFile2 ( process_handle, memory_mapped_view_address, flags) }
66
+ {
59
67
tracing:: error!(
60
- "Failed to free surrogate process resources (VirtualFreeEx failed): {:?}" ,
68
+ "Failed to free surrogate process resources (UnmapViewOfFile2 failed): {:?}" ,
61
69
e
62
70
) ;
63
71
}
@@ -66,9 +74,21 @@ impl Drop for SurrogateProcess {
66
74
// of the SurrogateProcess being dropped. this is ok to
67
75
// do because we are in the process of dropping ourselves
68
76
// anyway.
69
- get_surrogate_process_manager ( )
70
- . unwrap ( )
71
- . return_surrogate_process ( self . process_handle )
72
- . unwrap ( ) ;
77
+ match get_surrogate_process_manager ( ) {
78
+ Ok ( manager) => match manager. return_surrogate_process ( self . process_handle ) {
79
+ Ok ( _) => ( ) ,
80
+ Err ( e) => {
81
+ tracing:: error!( "Failed to return surrogate process to surrogate process manager when dropping : {:?}" , e) ;
82
+ return ;
83
+ }
84
+ } ,
85
+ Err ( e) => {
86
+ tracing:: error!(
87
+ "Failed to get surrogate process manager when dropping SurrogateProcess: {:?}" ,
88
+ e
89
+ ) ;
90
+ return ;
91
+ }
92
+ }
73
93
}
74
94
}
0 commit comments