Merge "Reduce risk of memory corruption due to finalization."

This commit is contained in:
Hans Boehm
2015-09-09 22:28:42 +00:00
committed by Gerrit Code Review
13 changed files with 20 additions and 7 deletions

View File

@ -147,11 +147,12 @@ public class Interpolator {
@Override @Override
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
nativeDestructor(native_instance); nativeDestructor(native_instance);
native_instance = 0; // Other finalizers can still call us.
} }
private int mValueCount; private int mValueCount;
private int mFrameCount; private int mFrameCount;
private final long native_instance; private long native_instance;
private static native long nativeConstructor(int valueCount, int frameCount); private static native long nativeConstructor(int valueCount, int frameCount);
private static native void nativeDestructor(long native_instance); private static native void nativeDestructor(long native_instance);

View File

@ -25,6 +25,7 @@ public class MaskFilter {
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
nativeDestructor(native_instance); nativeDestructor(native_instance);
native_instance = 0; // Other finalizers can still call us.
} }
private static native void nativeDestructor(long native_filter); private static native void nativeDestructor(long native_filter);

View File

@ -827,6 +827,7 @@ public class Matrix {
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
try { try {
finalizer(native_instance); finalizer(native_instance);
native_instance = 0; // Other finalizers can still call us.
} finally { } finally {
super.finalize(); super.finalize();
} }

View File

@ -71,7 +71,7 @@ public class NinePatch {
* *
* @hide * @hide
*/ */
public final long mNativeChunk; public long mNativeChunk;
private Paint mPaint; private Paint mPaint;
private String mSrcName; private String mSrcName;
@ -121,6 +121,7 @@ public class NinePatch {
if (mNativeChunk != 0) { if (mNativeChunk != 0) {
// only attempt to destroy correctly initilized chunks // only attempt to destroy correctly initilized chunks
nativeFinalize(mNativeChunk); nativeFinalize(mNativeChunk);
mNativeChunk = 0;
} }
} finally { } finally {
super.finalize(); super.finalize();

View File

@ -2219,6 +2219,7 @@ public class Paint {
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
try { try {
finalizer(mNativePaint); finalizer(mNativePaint);
mNativePaint = 0;
} finally { } finally {
super.finalize(); super.finalize();
} }

View File

@ -27,7 +27,7 @@ public class Path {
/** /**
* @hide * @hide
*/ */
public final long mNativePath; public long mNativePath;
/** /**
* @hide * @hide
@ -746,6 +746,7 @@ public class Path {
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
try { try {
finalizer(mNativePath); finalizer(mNativePath);
mNativePath = 0; // Other finalizers can still call us.
} finally { } finally {
super.finalize(); super.finalize();
} }

View File

@ -25,6 +25,7 @@ public class PathEffect {
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
nativeDestructor(native_instance); nativeDestructor(native_instance);
native_instance = 0; // Other finalizers can still call us.
} }
private static native void nativeDestructor(long native_patheffect); private static native void nativeDestructor(long native_patheffect);

View File

@ -142,6 +142,7 @@ public class PathMeasure {
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
native_destroy(native_instance); native_destroy(native_instance);
native_instance = 0; // Other finalizers can still call us.
} }
private static native long native_create(long native_path, boolean forceClosed); private static native long native_create(long native_path, boolean forceClosed);
@ -154,6 +155,6 @@ public class PathMeasure {
private static native boolean native_nextContour(long native_instance); private static native boolean native_nextContour(long native_instance);
private static native void native_destroy(long native_instance); private static native void native_destroy(long native_instance);
/* package */private final long native_instance; /* package */private long native_instance;
} }

View File

@ -29,7 +29,7 @@ import java.io.OutputStream;
*/ */
public class Picture { public class Picture {
private Canvas mRecordingCanvas; private Canvas mRecordingCanvas;
private final long mNativePicture; private long mNativePicture;
private static final int WORKING_STREAM_STORAGE = 16 * 1024; private static final int WORKING_STREAM_STORAGE = 16 * 1024;
@ -60,6 +60,7 @@ public class Picture {
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
try { try {
nativeDestructor(mNativePicture); nativeDestructor(mNativePicture);
mNativePicture = 0;
} finally { } finally {
super.finalize(); super.finalize();
} }

View File

@ -30,7 +30,7 @@ public class Region implements Parcelable {
/** /**
* @hide * @hide
*/ */
public final long mNativeRegion; public long mNativeRegion;
// the native values for these must match up with the enum in SkRegion.h // the native values for these must match up with the enum in SkRegion.h
public enum Op { public enum Op {
@ -380,6 +380,7 @@ public class Region implements Parcelable {
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
try { try {
nativeDestructor(mNativeRegion); nativeDestructor(mNativeRegion);
mNativeRegion = 0;
} finally { } finally {
super.finalize(); super.finalize();
} }

View File

@ -43,12 +43,13 @@ public class RegionIterator {
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
nativeDestructor(mNativeIter); nativeDestructor(mNativeIter);
mNativeIter = 0; // Other finalizers can still call us.
} }
private static native long nativeConstructor(long native_region); private static native long nativeConstructor(long native_region);
private static native void nativeDestructor(long native_iter); private static native void nativeDestructor(long native_iter);
private static native boolean nativeNext(long native_iter, Rect r); private static native boolean nativeNext(long native_iter, Rect r);
private final long mNativeIter; private long mNativeIter;
} }

View File

@ -90,6 +90,7 @@ public class Shader {
super.finalize(); super.finalize();
} finally { } finally {
nativeDestructor(native_instance); nativeDestructor(native_instance);
native_instance = 0; // Other finalizers can still call us.
} }
} }

View File

@ -358,6 +358,7 @@ public class Typeface {
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
try { try {
nativeUnref(native_instance); nativeUnref(native_instance);
native_instance = 0; // Other finalizers can still call us.
} finally { } finally {
super.finalize(); super.finalize();
} }