am e4d787b2: am 80e455e3: Merge "Prevent possible memory leak in SpanSet"

* commit 'e4d787b277a4b9435688c7e6f380bfc367751f76':
  Prevent possible memory leak in SpanSet
This commit is contained in:
Romain Guy
2015-04-08 07:27:58 +00:00
committed by Android Git Automerger

View File

@@ -17,6 +17,7 @@
package android.text;
import java.lang.reflect.Array;
import java.util.Arrays;
/**
* A cached set of spans. Caches the result of {@link Spanned#getSpans(int, int, Class)} and then
@@ -54,6 +55,7 @@ public class SpanSet<E> {
spanFlags = new int[length];
}
int prevNumberOfSpans = numberOfSpans;
numberOfSpans = 0;
for (int i = 0; i < length; i++) {
final E span = allSpans[i];
@@ -71,6 +73,12 @@ public class SpanSet<E> {
numberOfSpans++;
}
// cleanup extra spans left over from previous init() call
if (numberOfSpans < prevNumberOfSpans) {
// prevNumberofSpans was > 0, therefore spans != null
Arrays.fill(spans, numberOfSpans, prevNumberOfSpans, null);
}
}
/**
@@ -103,9 +111,8 @@ public class SpanSet<E> {
* Removes all internal references to the spans to avoid memory leaks.
*/
public void recycle() {
// The spans array is guaranteed to be not null when numberOfSpans is > 0
for (int i = 0; i < numberOfSpans; i++) {
spans[i] = null; // prevent a leak: no reference kept when TextLine is recycled
if (spans != null) {
Arrays.fill(spans, 0, numberOfSpans, null);
}
}
}