From 4566b79736f236c0f605c57130d1fa954f4642d6 Mon Sep 17 00:00:00 2001 From: Phil Dubach Date: Wed, 17 Jun 2009 15:27:42 -0700 Subject: Fix Canvas.finalize() for the case where the constructor throws an exception before the native canvas instance was created. If the canvas constructors throw an exception (because the bitmap passed in is immutable or already recycled), the constructor terminates early without allocating the native canvas instance. For the most part, that's okay, since the Canvas instance will never be returned to the application. However, the GC will still call finalize() on the half-initialized Canvas. The native methods for Canvas all assume that the canvas pointer passed down is not null. --- graphics/java/android/graphics/Canvas.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/java/android/graphics/Canvas.java b/graphics/java/android/graphics/Canvas.java index 06d53e3..4498e1a 100644 --- a/graphics/java/android/graphics/Canvas.java +++ b/graphics/java/android/graphics/Canvas.java @@ -1404,7 +1404,11 @@ public class Canvas { protected void finalize() throws Throwable { super.finalize(); - finalizer(mNativeCanvas); + // If the constructor threw an exception before setting mNativeCanvas, the native finalizer + // must not be invoked. + if (mNativeCanvas != 0) { + finalizer(mNativeCanvas); + } } /** -- cgit v1.1