Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bc999ddb authored by Josh Gao's avatar Josh Gao Committed by android-build-team Robot
Browse files

SharedMemory: break Cleaner reference cycle.

Previously, the Cleaner we create to close the ashmem file descriptor
used a thunk that held a strong reference to the FileDescriptor we
wanted to clean up, which prevented the Cleaner from ever running.

Break the cycle by storing the integer value of the file descriptor
instead.

Bug: http://b/138323667
Test: treehugger
Change-Id: I613a7d035892032f9567d59acb04672957c96011
(cherry picked from commit 6ca916a6)
(cherry picked from commit 390d9e6a)
parent 185fc7df
Loading
Loading
Loading
Loading
+6 −4
Original line number Original line Diff line number Diff line
@@ -62,7 +62,7 @@ public final class SharedMemory implements Parcelable, Closeable {


        mMemoryRegistration = new MemoryRegistration(mSize);
        mMemoryRegistration = new MemoryRegistration(mSize);
        mCleaner = Cleaner.create(mFileDescriptor,
        mCleaner = Cleaner.create(mFileDescriptor,
                new Closer(mFileDescriptor, mMemoryRegistration));
                new Closer(mFileDescriptor.getInt$(), mMemoryRegistration));
    }
    }


    /**
    /**
@@ -290,10 +290,10 @@ public final class SharedMemory implements Parcelable, Closeable {
     * Cleaner that closes the FD
     * Cleaner that closes the FD
     */
     */
    private static final class Closer implements Runnable {
    private static final class Closer implements Runnable {
        private FileDescriptor mFd;
        private int mFd;
        private MemoryRegistration mMemoryReference;
        private MemoryRegistration mMemoryReference;


        private Closer(FileDescriptor fd, MemoryRegistration memoryReference) {
        private Closer(int fd, MemoryRegistration memoryReference) {
            mFd = fd;
            mFd = fd;
            mMemoryReference = memoryReference;
            mMemoryReference = memoryReference;
        }
        }
@@ -301,7 +301,9 @@ public final class SharedMemory implements Parcelable, Closeable {
        @Override
        @Override
        public void run() {
        public void run() {
            try {
            try {
                Os.close(mFd);
                FileDescriptor fd = new FileDescriptor();
                fd.setInt$(mFd);
                Os.close(fd);
            } catch (ErrnoException e) { /* swallow error */ }
            } catch (ErrnoException e) { /* swallow error */ }
            mMemoryReference.release();
            mMemoryReference.release();
            mMemoryReference = null;
            mMemoryReference = null;