From 610f716b00f214e4899a102c1bbc1d6a323e114e Mon Sep 17 00:00:00 2001 From: "vandebo@chromium.org" Date: Wed, 14 Mar 2012 18:34:15 +0000 Subject: [PATCH] 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 --- include/core/SkClipStack.h | 2 +- src/core/SkClipStack.cpp | 14 +++++++++++--- src/pdf/SkPDFFont.cpp | 5 ++--- src/pdf/SkPDFStream.cpp | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) 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 @@ class SK_API SkClipStack { 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 @@ SkClipStack::SkClipStack(const SkClipStack& b) : fDeque(sizeof(Rec)) { *this = b; } +SkClipStack::~SkClipStack() { + reset(); +} + SkClipStack& SkClipStack::operator=(const SkClipStack& b) { if (this == &b) { return *this; @@ -119,9 +123,13 @@ bool SkClipStack::operator==(const SkClipStack& b) const { } 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 @@ static SkPDFStream* generate_tounicode_cmap( append_cmap_footer(&cmap); SkRefPtr 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 @@ SkPDFFont* SkPDFFont::GetFontResource(SkTypeface* typeface, uint16_t glyphID) { #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 @@ bool SkPDFStream::populate(SkPDFCatalog* catalog) { 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"); -- 2.7.4