Skip to content

Commit 3cab37a

Browse files
committed
Try fix FSEventStreamCreate issue
1 parent 4981531 commit 3cab37a

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/main/rust/src/fs_monitor.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,24 @@ impl NativeEventStream {
6767
}
6868

6969
fn build_context(&mut self) -> *const fsevent_sys::FSEventStreamContext {
70-
eprintln!("ctx sending: {?}", self.sender_heap);
70+
eprintln!("ctx sending: {:?}", self.sender_heap);
7171
&fs::FSEventStreamContext {
7272
version: 0,
7373
info: self.sender_heap as *mut _,
7474
retain: None,
75-
release: None,
75+
release: Some(release_context),
7676
copy_description: None
7777
}
7878
}
7979

8080
pub fn start(&mut self) {
8181
unsafe {
82+
let x = self.build_context();
8283
eprintln!("Creating stream: {}", self.since_when);
8384
let stream = fs::FSEventStreamCreate(
8485
kCFAllocatorDefault,
8586
callback,
86-
self.build_context(),
87+
x,
8788
self.path.as_concrete_TypeRef(),
8889
self.since_when,
8990
0.15,
@@ -109,7 +110,6 @@ impl NativeEventStream {
109110
fs::FSEventStreamInvalidate(stream);
110111
dispatch2::ffi::dispatch_release(self.queue as dispatch_object_t);
111112
fs::FSEventStreamRelease(stream);
112-
drop(Box::from(self.sender_heap));
113113
}
114114
None => unsafe {
115115
dispatch2::ffi::dispatch_release(self.queue as dispatch_object_t);
@@ -127,6 +127,20 @@ impl NativeEventStream {
127127

128128
}
129129

130+
extern "C" fn release_context(info: *mut c_void) {
131+
// Safety:
132+
// - The [documentation] for `FSEventStreamContext` states that `release` is only
133+
// called when the stream is deallocated, so it is safe to convert `info` back into a
134+
// box and drop it.
135+
//
136+
// [docs]: https://developer.apple.com/documentation/coreservices/fseventstreamcontext?language=objc
137+
unsafe {
138+
drop(Box::from_raw(
139+
info as *mut CallbackContext,
140+
));
141+
}
142+
}
143+
130144
extern "C" fn callback(
131145
_stream_ref: fs::FSEventStreamRef,
132146
info: *mut c_void,
@@ -136,7 +150,7 @@ extern "C" fn callback(
136150
event_ids: *const fs::FSEventStreamEventId,
137151
) {
138152
let ctx = unsafe{ &mut *(info as *mut CallbackContext) };
139-
eprintln!("ctx restored: {?}", ctx);
153+
eprintln!("ctx restored: {:?}", "foo");
140154

141155
let event_paths = event_paths as *const *const c_char;
142156

0 commit comments

Comments
 (0)