Fix four memory leaks uncovered by valgrinding gm tests.

All are triggered by PDF code.
 Two are missing unref's on SkData.
 One is a missing unref on a SkAdvancedTypefaceMetrics.
 The last is missing destruction of SkClipStack internal state.

BUG=526

Review URL: https://codereview.appspot.com/5824049

git-svn-id: http://skia.googlecode.com/svn/trunk@3386 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkClipStack.h b/include/core/SkClipStack.h
index fc96f03..306ba2a 100644
--- a/include/core/SkClipStack.h
+++ b/include/core/SkClipStack.h
@@ -18,7 +18,7 @@
 public:
     SkClipStack();
     SkClipStack(const SkClipStack& b);
-    ~SkClipStack() {}
+    ~SkClipStack();
 
     SkClipStack& operator=(const SkClipStack& b);
     bool operator==(const SkClipStack& b) const;
diff --git a/src/core/SkClipStack.cpp b/src/core/SkClipStack.cpp
index cfa9482..567fd7e 100644
--- a/src/core/SkClipStack.cpp
+++ b/src/core/SkClipStack.cpp
@@ -82,6 +82,10 @@
     *this = b;
 }
 
+SkClipStack::~SkClipStack() {
+    reset();
+}
+
 SkClipStack& SkClipStack::operator=(const SkClipStack& b) {
     if (this == &b) {
         return *this;
@@ -119,9 +123,13 @@
 }
 
 void SkClipStack::reset() {
-    // don't have a reset() on SkDeque, so fake it here
-    fDeque.~SkDeque();
-    new (&fDeque) SkDeque(sizeof(Rec));
+    // We used a placement new for each object in fDeque, so we're responsible
+    // for calling the destructor on each of them as well.
+    while (!fDeque.empty()) {
+        Rec* rec = (Rec*)fDeque.back();
+        rec->~Rec();
+        fDeque.pop_back();
+    }
 
     fSaveCount = 0;
 }
diff --git a/src/pdf/SkPDFFont.cpp b/src/pdf/SkPDFFont.cpp
index 3aea4b8..e0e18cd 100644
--- a/src/pdf/SkPDFFont.cpp
+++ b/src/pdf/SkPDFFont.cpp
@@ -525,7 +525,7 @@
     append_cmap_footer(&cmap);
     SkRefPtr<SkMemoryStream> cmapStream = new SkMemoryStream();
     cmapStream->unref();  // SkRefPtr and new took a reference.
-    cmapStream->setData(cmap.copyToData());
+    cmapStream->setData(cmap.copyToData())->unref();
     return new SkPDFStream(cmapStream.get());
 }
 
@@ -763,9 +763,8 @@
 #endif
         fontMetrics =
             SkFontHost::GetAdvancedTypefaceMetrics(fontID, info, NULL, 0);
-#if defined (SK_SFNTLY_SUBSETTER)
-        SkASSERT(fontMetrics);
         SkSafeUnref(fontMetrics.get());  // SkRefPtr and Get both took a ref.
+#if defined (SK_SFNTLY_SUBSETTER)
         if (fontMetrics &&
             fontMetrics->fType != SkAdvancedTypefaceMetrics::kTrueType_Font) {
             // Font does not support subsetting, get new info with advance.
diff --git a/src/pdf/SkPDFStream.cpp b/src/pdf/SkPDFStream.cpp
index 0bd63f3..49c7156 100644
--- a/src/pdf/SkPDFStream.cpp
+++ b/src/pdf/SkPDFStream.cpp
@@ -94,7 +94,7 @@
             SkAssertResult(SkFlate::Deflate(fData.get(), &compressedData));
             if (compressedData.getOffset() < fData->getLength()) {
                 SkMemoryStream* stream = new SkMemoryStream;
-                stream->setData(compressedData.copyToData());
+                stream->setData(compressedData.copyToData())->unref();
                 fData = stream;
                 fData->unref();  // SkRefPtr and new both took a reference.
                 insertName("Filter", "FlateDecode");