From e3f9800f38da3cd1d83889132952eb4ab2e8c278 Mon Sep 17 00:00:00 2001 From: Leon Scroggins III Date: Tue, 10 Jun 2014 13:19:35 -0400 Subject: [PATCH] Fix a memory leak with empty screenshot. Use an autodeleter to delete the ScreenshotClient when it's not needed. Discovered while investigating BUG:15454296, but it does not fix the problem. Change-Id: I00ea81de15ddc2507d904a6c20af3c410f573dce --- core/jni/android_view_SurfaceControl.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/jni/android_view_SurfaceControl.cpp b/core/jni/android_view_SurfaceControl.cpp index 4594cc31adac4..8e6c4a0c36212 100644 --- a/core/jni/android_view_SurfaceControl.cpp +++ b/core/jni/android_view_SurfaceControl.cpp @@ -42,6 +42,8 @@ #include +#include "SkTemplates.h" + // ---------------------------------------------------------------------------- namespace android { @@ -125,7 +127,7 @@ static jobject nativeScreenshotBitmap(JNIEnv* env, jclass clazz, int bottom = env->GetIntField(sourceCropObj, gRectClassInfo.bottom); Rect sourceCrop(left, top, right, bottom); - ScreenshotClient* screenshot = new ScreenshotClient(); + SkAutoTDelete screenshot(new ScreenshotClient()); status_t res; if (width > 0 && height > 0) { if (allLayers) { @@ -139,7 +141,6 @@ static jobject nativeScreenshotBitmap(JNIEnv* env, jclass clazz, res = screenshot->update(displayToken, sourceCrop, useIdentityTransform); } if (res != NO_ERROR) { - delete screenshot; return NULL; } @@ -164,7 +165,6 @@ static jobject nativeScreenshotBitmap(JNIEnv* env, jclass clazz, break; } default: { - delete screenshot; return NULL; } } @@ -178,7 +178,7 @@ static jobject nativeScreenshotBitmap(JNIEnv* env, jclass clazz, // takes ownership of ScreenshotClient SkMallocPixelRef* pixels = SkMallocPixelRef::NewWithProc(screenshotInfo, (size_t) rowBytes, NULL, (void*) screenshot->getPixels(), &DeleteScreenshot, - (void*) screenshot); + (void*) (screenshot.detach())); pixels->setImmutable(); bitmap->setPixelRef(pixels)->unref(); bitmap->lockPixels();