Skip to content

Commit 76c9545

Browse files
kstangerronshapiro
authored andcommitted
Use J2ObjC's @RetainedWith annotation instead of @weak on ImmutableMap.keySet
to prevent crashes on iOS. TESTED=Created a map inside a autorelease pool and saved a reference to the keyset. Checked that the keyset functions correctly outside the autorelease pool. Checked that both the keyset and the map are deallocated by the autorelease pool when not holding a reference to the keyset. Did this test with both ImmutableMap.of(<2 entries>) and ImmutableBiMap.of(<2 entries>). RELNOTES=J2ObjC: Fixes crashes from use of `ImmutableMap.keySet()`. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=233811320
1 parent 46a2123 commit 76c9545

File tree

5 files changed

+7
-7
lines changed

5 files changed

+7
-7
lines changed

android/guava/src/com/google/common/collect/ImmutableMap.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.common.annotations.GwtCompatible;
2626
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2727
import com.google.errorprone.annotations.concurrent.LazyInit;
28+
import com.google.j2objc.annotations.RetainedWith;
2829
import com.google.j2objc.annotations.WeakOuter;
2930
import java.io.Serializable;
3031
import java.util.AbstractMap;
@@ -537,7 +538,7 @@ public ImmutableSet<Entry<K, V>> entrySet() {
537538

538539
abstract ImmutableSet<Entry<K, V>> createEntrySet();
539540

540-
@LazyInit private transient ImmutableSet<K> keySet;
541+
@LazyInit @RetainedWith private transient ImmutableSet<K> keySet;
541542

542543
/**
543544
* Returns an immutable set of the keys in this map, in the same order that they appear in {@link

android/guava/src/com/google/common/collect/ImmutableMapKeySet.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import com.google.common.annotations.GwtCompatible;
2020
import com.google.common.annotations.GwtIncompatible;
21-
import com.google.j2objc.annotations.Weak;
2221
import java.io.Serializable;
2322
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
2423

@@ -30,7 +29,7 @@
3029
*/
3130
@GwtCompatible(emulated = true)
3231
final class ImmutableMapKeySet<K, V> extends IndexedImmutableSet<K> {
33-
@Weak private final ImmutableMap<K, V> map;
32+
private final ImmutableMap<K, V> map;
3433

3534
ImmutableMapKeySet(ImmutableMap<K, V> map) {
3635
this.map = map;

guava/src/com/google/common/collect/ImmutableMap.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.common.annotations.VisibleForTesting;
2727
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2828
import com.google.errorprone.annotations.concurrent.LazyInit;
29+
import com.google.j2objc.annotations.RetainedWith;
2930
import com.google.j2objc.annotations.WeakOuter;
3031
import java.io.Serializable;
3132
import java.util.AbstractMap;
@@ -718,7 +719,7 @@ public ImmutableSet<Entry<K, V>> entrySet() {
718719

719720
abstract ImmutableSet<Entry<K, V>> createEntrySet();
720721

721-
@LazyInit private transient ImmutableSet<K> keySet;
722+
@LazyInit @RetainedWith private transient ImmutableSet<K> keySet;
722723

723724
/**
724725
* Returns an immutable set of the keys in this map, in the same order that they appear in {@link

guava/src/com/google/common/collect/ImmutableMapKeySet.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import com.google.common.annotations.GwtCompatible;
2222
import com.google.common.annotations.GwtIncompatible;
23-
import com.google.j2objc.annotations.Weak;
2423
import java.io.Serializable;
2524
import java.util.Spliterator;
2625
import java.util.function.Consumer;
@@ -34,7 +33,7 @@
3433
*/
3534
@GwtCompatible(emulated = true)
3635
final class ImmutableMapKeySet<K, V> extends IndexedImmutableSet<K> {
37-
@Weak private final ImmutableMap<K, V> map;
36+
private final ImmutableMap<K, V> map;
3837

3938
ImmutableMapKeySet(ImmutableMap<K, V> map) {
4039
this.map = map;

guava/src/com/google/common/collect/RegularImmutableMap.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ ImmutableSet<K> createKeySet() {
209209

210210
@GwtCompatible(emulated = true)
211211
private static final class KeySet<K, V> extends IndexedImmutableSet<K> {
212-
@Weak private final RegularImmutableMap<K, V> map;
212+
private final RegularImmutableMap<K, V> map;
213213

214214
KeySet(RegularImmutableMap<K, V> map) {
215215
this.map = map;

0 commit comments

Comments
 (0)