Skip to content

Commit c22ca44

Browse files
committed
Fix confusing comments about deallocation
1 parent 3589680 commit c22ca44

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/main/java/engineering/swat/watch/impl/mac/NativeEventStream.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff 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) {

src/test/java/engineering/swat/watch/impl/mac/APIs.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)