be sure to unlock the discardablememory before deleting it

BUG=

Review URL: https://codereview.chromium.org/114673002

git-svn-id: http://skia.googlecode.com/svn/trunk@12658 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/lazy/SkDiscardableMemoryPool.cpp b/src/lazy/SkDiscardableMemoryPool.cpp
index a1b2438..4709709 100644
--- a/src/lazy/SkDiscardableMemoryPool.cpp
+++ b/src/lazy/SkDiscardableMemoryPool.cpp
@@ -47,19 +47,23 @@
 }
 
 SkPoolDiscardableMemory::~SkPoolDiscardableMemory() {
+    SkASSERT(!fLocked); // contract for SkDiscardableMemory
     fPool->free(this);
     fPool->unref();
 }
 
 bool SkPoolDiscardableMemory::lock() {
+    SkASSERT(!fLocked); // contract for SkDiscardableMemory
     return fPool->lock(this);
 }
 
 void* SkPoolDiscardableMemory::data() {
-    return fLocked ? fPointer : NULL;
+    SkASSERT(fLocked); // contract for SkDiscardableMemory
+    return fPointer;
 }
 
 void SkPoolDiscardableMemory::unlock() {
+    SkASSERT(fLocked); // contract for SkDiscardableMemory
     fPool->unlock(this);
 }
 
diff --git a/src/lazy/SkDiscardablePixelRef.cpp b/src/lazy/SkDiscardablePixelRef.cpp
index 6a9507c..0b193ad 100644
--- a/src/lazy/SkDiscardablePixelRef.cpp
+++ b/src/lazy/SkDiscardablePixelRef.cpp
@@ -30,6 +30,9 @@
 }
 
 SkDiscardablePixelRef::~SkDiscardablePixelRef() {
+    if (this->isLocked()) {
+        fDiscardableMemory->unlock();
+    }
     SkDELETE(fDiscardableMemory);
     SkSafeUnref(fDMFactory);
     SkDELETE(fGenerator);
diff --git a/tests/CachedDecodingPixelRefTest.cpp b/tests/CachedDecodingPixelRefTest.cpp
index f97e893..04eddd9 100644
--- a/tests/CachedDecodingPixelRefTest.cpp
+++ b/tests/CachedDecodingPixelRefTest.cpp
@@ -268,12 +268,25 @@
     }
 }
 }  // namespace
+
+// new/lock/delete is an odd pattern for a pixelref, but it needs to not assert
+static void test_newlockdelete(skiatest::Reporter* reporter) {
+    SkBitmap bm;
+    SkImageGenerator* ig = new TestImageGenerator(
+                                 TestImageGenerator::kSucceedGetPixels_TestType,
+                                 reporter);
+    SkInstallDiscardablePixelRef(ig, &bm, NULL);
+    bm.pixelRef()->lockPixels();
+}
+
 /**
  *  This tests the basic functionality of SkDiscardablePixelRef with a
  *  basic SkImageGenerator implementation and several
  *  SkDiscardableMemory::Factory choices.
  */
 DEF_TEST(DiscardableAndCachingPixelRef, reporter) {
+    test_newlockdelete(reporter);
+
     CheckPixelRef(TestImageGenerator::kFailGetInfo_TestType,
                   reporter, kSkCaching_PixelRefType, NULL);
     CheckPixelRef(TestImageGenerator::kFailGetPixels_TestType,