From f4ed20e9b3eabaacfa165bf532f67c19f22809f9 Mon Sep 17 00:00:00 2001 From: kstanger Date: Fri, 22 Feb 2019 09:39:55 -0800 Subject: [PATCH] Use J2ObjC's @RetainedWith annotation instead of @Weak on ImmutableMap.values to prevent crashes on iOS. TESTED=Created a map inside an autorelease pool and saved a reference to the values collection. Checked that the values collection functions correctly outside the autorelease pool. Checked that both the values collection and the map are deallocated by the autorelease pool when not holding a strong reference to the values collection. Did this test with ImmutableMap.of(<2 entries>) and ImmutableMap.copyOf(). RELNOTES=J2ObjC: Fixes crashes from use of `ImmutableMap.values()`. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=235206171 --- android/guava/src/com/google/common/collect/ImmutableMap.java | 2 +- .../src/com/google/common/collect/ImmutableMapValues.java | 3 +-- guava/src/com/google/common/collect/ImmutableMap.java | 2 +- guava/src/com/google/common/collect/ImmutableMapValues.java | 3 +-- guava/src/com/google/common/collect/RegularImmutableMap.java | 3 +-- 5 files changed, 5 insertions(+), 8 deletions(-) diff --git a/android/guava/src/com/google/common/collect/ImmutableMap.java b/android/guava/src/com/google/common/collect/ImmutableMap.java index a25c423385dc..1f25b7942680 100644 --- a/android/guava/src/com/google/common/collect/ImmutableMap.java +++ b/android/guava/src/com/google/common/collect/ImmutableMap.java @@ -572,7 +572,7 @@ public K next() { }; } - @LazyInit private transient ImmutableCollection values; + @LazyInit @RetainedWith private transient ImmutableCollection values; /** * Returns an immutable collection of the values in this map, in the same order that they appear diff --git a/android/guava/src/com/google/common/collect/ImmutableMapValues.java b/android/guava/src/com/google/common/collect/ImmutableMapValues.java index 2c94e5c4b210..4a0e3960d581 100644 --- a/android/guava/src/com/google/common/collect/ImmutableMapValues.java +++ b/android/guava/src/com/google/common/collect/ImmutableMapValues.java @@ -18,7 +18,6 @@ import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; -import com.google.j2objc.annotations.Weak; import java.io.Serializable; import java.util.Map.Entry; import org.checkerframework.checker.nullness.compatqual.NullableDecl; @@ -31,7 +30,7 @@ */ @GwtCompatible(emulated = true) final class ImmutableMapValues extends ImmutableCollection { - @Weak private final ImmutableMap map; + private final ImmutableMap map; ImmutableMapValues(ImmutableMap map) { this.map = map; diff --git a/guava/src/com/google/common/collect/ImmutableMap.java b/guava/src/com/google/common/collect/ImmutableMap.java index 0f6562b92ec0..0a376ed9cfeb 100644 --- a/guava/src/com/google/common/collect/ImmutableMap.java +++ b/guava/src/com/google/common/collect/ImmutableMap.java @@ -757,7 +757,7 @@ Spliterator keySpliterator() { return CollectSpliterators.map(entrySet().spliterator(), Entry::getKey); } - @LazyInit private transient ImmutableCollection values; + @LazyInit @RetainedWith private transient ImmutableCollection values; /** * Returns an immutable collection of the values in this map, in the same order that they appear diff --git a/guava/src/com/google/common/collect/ImmutableMapValues.java b/guava/src/com/google/common/collect/ImmutableMapValues.java index 6ccf98f6a26b..2c13c472c6bd 100644 --- a/guava/src/com/google/common/collect/ImmutableMapValues.java +++ b/guava/src/com/google/common/collect/ImmutableMapValues.java @@ -20,7 +20,6 @@ import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; -import com.google.j2objc.annotations.Weak; import java.io.Serializable; import java.util.Map.Entry; import java.util.Spliterator; @@ -35,7 +34,7 @@ */ @GwtCompatible(emulated = true) final class ImmutableMapValues extends ImmutableCollection { - @Weak private final ImmutableMap map; + private final ImmutableMap map; ImmutableMapValues(ImmutableMap map) { this.map = map; diff --git a/guava/src/com/google/common/collect/RegularImmutableMap.java b/guava/src/com/google/common/collect/RegularImmutableMap.java index f78180daee9f..3c74dc2d967c 100644 --- a/guava/src/com/google/common/collect/RegularImmutableMap.java +++ b/guava/src/com/google/common/collect/RegularImmutableMap.java @@ -26,7 +26,6 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableMapEntry.NonTerminalImmutableMapEntry; import com.google.errorprone.annotations.CanIgnoreReturnValue; -import com.google.j2objc.annotations.Weak; import java.io.Serializable; import java.util.function.BiConsumer; import org.checkerframework.checker.nullness.qual.Nullable; @@ -264,7 +263,7 @@ ImmutableCollection createValues() { @GwtCompatible(emulated = true) private static final class Values extends ImmutableList { - @Weak final RegularImmutableMap map; + final RegularImmutableMap map; Values(RegularImmutableMap map) { this.map = map;