File tree Expand file tree Collapse file tree 2 files changed +11
-7
lines changed
main/java/engineering/swat/watch/impl/mac
test/java/engineering/swat/watch/impl/mac Expand file tree Collapse file tree 2 files changed +11
-7
lines changed Original file line number Diff line number Diff line change @@ -84,10 +84,12 @@ class NativeEventStream implements Closeable {
8484 private static final DispatchQueue DQ = DispatchQueue .INSTANCE ;
8585 private static final FileSystemEvents FSE = FileSystemEvents .INSTANCE ;
8686
87- // Native memory (automatically deallocated when set to `null`)
88- private @ Nullable FSEventStreamCallback callback ;
87+ // Native memory
88+ private @ Nullable FSEventStreamCallback callback ; // Keep reference to avoid premature GC'ing
8989 private @ Nullable Pointer stream ;
9090 private @ Nullable Pointer queue ;
91+ // Note: These fields aren't volatile, as all reads/write from/to them are
92+ // inside synchronized blocks. Be careful to not break this invariant.
9193
9294 private final Path path ;
9395 private final NativeEventHandler handler ;
@@ -191,6 +193,8 @@ public synchronized void close() {
191193 FSE .FSEventStreamStop (streamNonNull );
192194 FSE .FSEventStreamSetDispatchQueue (streamNonNull , Pointer .NULL );
193195 FSE .FSEventStreamInvalidate (streamNonNull );
196+
197+ // Deallocate native memory
194198 FSE .FSEventStreamRelease (streamNonNull );
195199 }
196200 if (queue != null ) {
Original file line number Diff line number Diff line change @@ -192,13 +192,13 @@ public void close() throws IOException {
192192 FSE .FSEventStreamStop (stream );
193193 FSE .FSEventStreamSetDispatchQueue (stream , Pointer .NULL );
194194 FSE .FSEventStreamInvalidate (stream );
195- FSE .FSEventStreamRelease (stream );
196- DO .dispatch_release (queue );
197195
198196 // Deallocate queue, stream, and callback
199- this .queue = null ;
200- this .stream = null ;
201- this .callback = null ;
197+ DO .dispatch_release (queue );
198+ FSE .FSEventStreamRelease (stream );
199+ queue = null ;
200+ stream = null ;
201+ callback = null ;
202202 }
203203
204204 @ FunctionalInterface
You can’t perform that action at this time.
0 commit comments