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 {
84
84
private static final DispatchQueue DQ = DispatchQueue .INSTANCE ;
85
85
private static final FileSystemEvents FSE = FileSystemEvents .INSTANCE ;
86
86
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
89
89
private @ Nullable Pointer stream ;
90
90
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.
91
93
92
94
private final Path path ;
93
95
private final NativeEventHandler handler ;
@@ -191,6 +193,8 @@ public synchronized void close() {
191
193
FSE .FSEventStreamStop (streamNonNull );
192
194
FSE .FSEventStreamSetDispatchQueue (streamNonNull , Pointer .NULL );
193
195
FSE .FSEventStreamInvalidate (streamNonNull );
196
+
197
+ // Deallocate native memory
194
198
FSE .FSEventStreamRelease (streamNonNull );
195
199
}
196
200
if (queue != null ) {
Original file line number Diff line number Diff line change @@ -192,13 +192,13 @@ public void close() throws IOException {
192
192
FSE .FSEventStreamStop (stream );
193
193
FSE .FSEventStreamSetDispatchQueue (stream , Pointer .NULL );
194
194
FSE .FSEventStreamInvalidate (stream );
195
- FSE .FSEventStreamRelease (stream );
196
- DO .dispatch_release (queue );
197
195
198
196
// 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 ;
202
202
}
203
203
204
204
@ FunctionalInterface
You can’t perform that action at this time.
0 commit comments