Skip to content

Commit

Permalink
Use J2ObjC's @RetainedWith annotation instead of @weak on ImmutableMa…
Browse files Browse the repository at this point in the history
…p.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
  • Loading branch information
kstanger authored and ronshapiro committed Feb 14, 2019
1 parent b937ab0 commit 31ade17
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 7 deletions.
Expand Up @@ -25,6 +25,7 @@
import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.concurrent.LazyInit;
import com.google.j2objc.annotations.RetainedWith;
import com.google.j2objc.annotations.WeakOuter;
import java.io.Serializable;
import java.util.AbstractMap;
Expand Down Expand Up @@ -537,7 +538,7 @@ public ImmutableSet<Entry<K, V>> entrySet() {

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

@LazyInit private transient ImmutableSet<K> keySet;
@LazyInit @RetainedWith private transient ImmutableSet<K> keySet;

/**
* Returns an immutable set of the keys in this map, in the same order that they appear in {@link
Expand Down
Expand Up @@ -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 org.checkerframework.checker.nullness.compatqual.NullableDecl;

Expand All @@ -30,7 +29,7 @@
*/
@GwtCompatible(emulated = true)
final class ImmutableMapKeySet<K, V> extends IndexedImmutableSet<K> {
@Weak private final ImmutableMap<K, V> map;
private final ImmutableMap<K, V> map;

ImmutableMapKeySet(ImmutableMap<K, V> map) {
this.map = map;
Expand Down
3 changes: 2 additions & 1 deletion guava/src/com/google/common/collect/ImmutableMap.java
Expand Up @@ -26,6 +26,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.concurrent.LazyInit;
import com.google.j2objc.annotations.RetainedWith;
import com.google.j2objc.annotations.WeakOuter;
import java.io.Serializable;
import java.util.AbstractMap;
Expand Down Expand Up @@ -718,7 +719,7 @@ public ImmutableSet<Entry<K, V>> entrySet() {

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

@LazyInit private transient ImmutableSet<K> keySet;
@LazyInit @RetainedWith private transient ImmutableSet<K> keySet;

/**
* Returns an immutable set of the keys in this map, in the same order that they appear in {@link
Expand Down
3 changes: 1 addition & 2 deletions guava/src/com/google/common/collect/ImmutableMapKeySet.java
Expand Up @@ -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.Spliterator;
import java.util.function.Consumer;
Expand All @@ -34,7 +33,7 @@
*/
@GwtCompatible(emulated = true)
final class ImmutableMapKeySet<K, V> extends IndexedImmutableSet<K> {
@Weak private final ImmutableMap<K, V> map;
private final ImmutableMap<K, V> map;

ImmutableMapKeySet(ImmutableMap<K, V> map) {
this.map = map;
Expand Down
Expand Up @@ -209,7 +209,7 @@ ImmutableSet<K> createKeySet() {

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

KeySet(RegularImmutableMap<K, V> map) {
this.map = map;
Expand Down

0 comments on commit 31ade17

Please sign in to comment.