Skip to content

Commit

Permalink
Fix memory leak in SingletonImmutableBiMap which would appear in tran…
Browse files Browse the repository at this point in the history
…spiled J2ObjC code.

The @RetainedWith annotation can not be used on both sides of the retain-cycle. The RegularImmutableBiMap does not have this problem, because it uses Inverse inner class. This CL applies similar trick in SingletonImmutableBiMap, although without additional inner class.

RELNOTES=Update SingletonImmutableBiMap to avoid retain-cycle in transpiled Obj-C code.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=317856944
  • Loading branch information
micapolos authored and kluever committed Jun 23, 2020
1 parent 6cc0bb9 commit 0ad38b8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cycle_whitelist.txt
Expand Up @@ -13,8 +13,6 @@ NAMESPACE junit.framework
NAMESPACE org.junit

# ***** REAL CYCLES *****
# Inverses (currently not solvable by weakening a reference)
FIELD com.google.common.base.Converter.reverse
# Cycle exists until future completes
FIELD com.google.common.util.concurrent.AbstractFuture.Listener.executor com.google.common.util.concurrent.ExecutionSequencer.TaskNonReentrantExecutor

Expand All @@ -23,6 +21,7 @@ FIELD com.google.common.util.concurrent.AbstractFuture.Listener.executor com.goo
# The Runnable type is so generic that it produces too many false positives.
TYPE java.lang.Runnable

FIELD com.google.common.base.Converter.reverse
FIELD com.google.common.collect.AbstractBiMap.EntrySet.iterator.$.entry com.google.common.collect.AbstractBiMap.EntrySet.iterator.$.next.$
FIELD com.google.common.collect.AbstractMapBasedMultimap.map
FIELD com.google.common.collect.AbstractMultimap.asMap com.google.common.collect.AbstractMapBasedMultimap.NavigableAsMap
Expand All @@ -36,6 +35,7 @@ FIELD com.google.common.collect.ImmutableRangeSet.ranges
FIELD com.google.common.collect.ImmutableSet.asList
FIELD com.google.common.collect.Maps.FilteredMapValues.unfiltered
FIELD com.google.common.collect.Sets.SubSet.inputSet
FIELD com.google.common.collect.SingletonImmutableBiMap.inverse
FIELD com.google.common.collect.TreeTraverser.PostOrderNode.childIterator
FIELD com.google.common.collect.TreeTraverser.PreOrderIterator.stack
FIELD com.google.common.util.concurrent.AbstractFuture.Listener.executor com.google.common.util.concurrent.MoreExecutors.rejectionPropagatingExecutor.$
Expand Down
18 changes: 12 additions & 6 deletions guava/src/com/google/common/collect/SingletonImmutableBiMap.java
Expand Up @@ -42,6 +42,7 @@ final class SingletonImmutableBiMap<K, V> extends ImmutableBiMap<K, V> {
checkEntryNotNull(singleKey, singleValue);
this.singleKey = singleKey;
this.singleValue = singleValue;
this.inverse = null;
}

private SingletonImmutableBiMap(K singleKey, V singleValue, ImmutableBiMap<V, K> inverse) {
Expand Down Expand Up @@ -90,16 +91,21 @@ ImmutableSet<K> createKeySet() {
return ImmutableSet.of(singleKey);
}

@LazyInit @RetainedWith transient ImmutableBiMap<V, K> inverse;
private final transient @Nullable ImmutableBiMap<V, K> inverse;
@LazyInit @RetainedWith private transient @Nullable ImmutableBiMap<V, K> lazyInverse;

@Override
public ImmutableBiMap<V, K> inverse() {
// racy single-check idiom
ImmutableBiMap<V, K> result = inverse;
if (result == null) {
return inverse = new SingletonImmutableBiMap<>(singleValue, singleKey, this);
if (inverse != null) {
return inverse;
} else {
return result;
// racy single-check idiom
ImmutableBiMap<V, K> result = lazyInverse;
if (result == null) {
return lazyInverse = new SingletonImmutableBiMap<>(singleValue, singleKey, this);
} else {
return result;
}
}
}
}

0 comments on commit 0ad38b8

Please sign in to comment.