diff options
author | robertphillips@google.com <robertphillips@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-11-18 16:26:25 +0000 |
---|---|---|
committer | robertphillips@google.com <robertphillips@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-11-18 16:26:25 +0000 |
commit | 701b40543d5d124dfa1e59b051cba9d2aaf61670 (patch) | |
tree | 25383c51b0a3dd19ce67fb947ad74885bb360af0 | |
parent | e61a86cfa00ea393ecc4a71fca94e1d476a37ecc (diff) |
Fix DocumentTest/SkDocument memory leaks
https://codereview.chromium.org/72833002/
git-svn-id: http://skia.googlecode.com/svn/trunk@12302 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r-- | include/core/SkDocument.h | 9 | ||||
-rw-r--r-- | src/doc/SkDocument.cpp | 12 | ||||
-rw-r--r-- | src/doc/SkDocument_PDF.cpp | 6 | ||||
-rw-r--r-- | tests/DocumentTest.cpp | 2 |
4 files changed, 20 insertions, 9 deletions
diff --git a/include/core/SkDocument.h b/include/core/SkDocument.h index 66946fe7c5..83b4e7a3ee 100644 --- a/include/core/SkDocument.h +++ b/include/core/SkDocument.h @@ -64,6 +64,9 @@ public: * The proc can delete the stream, or whatever it needs to do. * encoder sets the DCTEncoder for images, to encode a bitmap * as JPEG (DCT). + * Done - clean up method intended to allow deletion of the stream. + * Its aborted parameter is true if the cleanup is due to an abort + * call. It is false otherwise. * rasterDpi - the DPI at which features without native PDF support * will be rasterized (e.g. draw image with perspective, * draw text with perspective, ...) @@ -73,7 +76,7 @@ public: * and it would be slower to be processed or sent online or * to printer. */ static SkDocument* CreatePDF( - SkWStream*, void (*Done)(SkWStream*) = NULL, + SkWStream*, void (*Done)(SkWStream*,bool aborted) = NULL, SkPicture::EncodeBitmap encoder = NULL, SkScalar rasterDpi = SK_ScalarDefaultRasterDPI); @@ -108,7 +111,7 @@ public: void abort(); protected: - SkDocument(SkWStream*, void (*)(SkWStream*)); + SkDocument(SkWStream*, void (*)(SkWStream*, bool aborted)); // note: subclasses must call close() in their destructor, as the base class // cannot do this for them. virtual ~SkDocument(); @@ -128,7 +131,7 @@ protected: private: SkWStream* fStream; - void (*fDoneProc)(SkWStream*); + void (*fDoneProc)(SkWStream*, bool aborted); State fState; typedef SkRefCnt INHERITED; diff --git a/src/doc/SkDocument.cpp b/src/doc/SkDocument.cpp index cefe34ee85..85a2bad5e8 100644 --- a/src/doc/SkDocument.cpp +++ b/src/doc/SkDocument.cpp @@ -10,7 +10,7 @@ SK_DEFINE_INST_COUNT(SkDocument) -SkDocument::SkDocument(SkWStream* stream, void (*doneProc)(SkWStream*)) { +SkDocument::SkDocument(SkWStream* stream, void (*doneProc)(SkWStream*, bool)) { fStream = stream; // we do not own this object. fDoneProc = doneProc; fState = kBetweenPages_State; @@ -68,7 +68,7 @@ bool SkDocument::close() { bool success = this->onClose(fStream); if (fDoneProc) { - fDoneProc(fStream); + fDoneProc(fStream, false); } // we don't own the stream, but we mark it NULL since we can // no longer write to it. @@ -85,5 +85,13 @@ bool SkDocument::close() { } void SkDocument::abort() { + this->onAbort(); + fState = kClosed_State; + if (fDoneProc) { + fDoneProc(fStream, true); + } + // we don't own the stream, but we mark it NULL since we can + // no longer write to it. + fStream = NULL; } diff --git a/src/doc/SkDocument_PDF.cpp b/src/doc/SkDocument_PDF.cpp index 362493d538..142099375c 100644 --- a/src/doc/SkDocument_PDF.cpp +++ b/src/doc/SkDocument_PDF.cpp @@ -11,7 +11,7 @@ class SkDocument_PDF : public SkDocument { public: - SkDocument_PDF(SkWStream* stream, void (*doneProc)(SkWStream*), + SkDocument_PDF(SkWStream* stream, void (*doneProc)(SkWStream*,bool), SkPicture::EncodeBitmap encoder, SkScalar rasterDpi) : SkDocument(stream, doneProc) @@ -86,13 +86,13 @@ private: /////////////////////////////////////////////////////////////////////////////// -SkDocument* SkDocument::CreatePDF(SkWStream* stream, void (*done)(SkWStream*), +SkDocument* SkDocument::CreatePDF(SkWStream* stream, void (*done)(SkWStream*,bool), SkPicture::EncodeBitmap enc, SkScalar dpi) { return stream ? SkNEW_ARGS(SkDocument_PDF, (stream, done, enc, dpi)) : NULL; } -static void delete_wstream(SkWStream* stream) { +static void delete_wstream(SkWStream* stream, bool aborted) { SkDELETE(stream); } diff --git a/tests/DocumentTest.cpp b/tests/DocumentTest.cpp index 1838687c4d..a717ac7de3 100644 --- a/tests/DocumentTest.cpp +++ b/tests/DocumentTest.cpp @@ -9,7 +9,7 @@ static void test_empty(skiatest::Reporter* reporter) { SkDynamicMemoryWStream stream; - SkDocument* doc = SkDocument::CreatePDF(&stream); + SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream)); doc->close(); |