google / google/j2objc

FileChannelImpl leak fix in #604 requires the channel's parent stream to be long-lived

Open
#728 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
6k
Forks
999
Avg merge
19h 20m
Merged PRs (30d)
14

Description

PR #604 fixed the leak created by obtaining a `FileChannel` as the channel's `stream` field refers to the source, which in turn also retains the channel (see [`RandomAccessFile`](https://github.com/google/j2objc/blob/master/jre_emul/android/libcore/luni/src/main/java/java/io/RandomAccessFile.java#L54) and [`FileChannelImpl`](https://github.com/google/j2objc/blob/master/jre_emul/android/libcore/luni/src/main/java/java/nio/FileChannelImpl.java#L55) for an example). After the fix, however, the source object is now required to be retained somewhere else.

For example, this is valid Java:

``` java
FileChannel getRWChannel(File file) {
RandomAccessFile raf = new RandomAccessFile(file, "rw");
return raf.getChannel();
}
```

The method returns the obtained channel and forgets about the source `RandomAccessFile`. With #604 in place, however, this code will no longer be valid, since the channel's `stream` field will be zeroed out when the autorelease pool is drained. It now requires the `raf` to be retained somewhere else – for example, by making the method to return a pair of `RandomAccessFile` and `FileChannel`, but that's clunky and not always feasible in a long-established code base.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.