Index: src/pdf/SkPDFTypes.h |
=================================================================== |
--- src/pdf/SkPDFTypes.h (revision 8010) |
+++ src/pdf/SkPDFTypes.h (working copy) |
@@ -29,6 +29,8 @@ |
public: |
SK_DECLARE_INST_COUNT(SkPDFObject) |
+ virtual ~SkPDFObject() { }; |
+ |
/** Return the size (number of bytes) of this object in the final output |
* file. Compound objects or objects that are computationally intensive |
* to output should override this method. |
@@ -79,6 +81,25 @@ |
static void GetResourcesHelper(SkTDArray<SkPDFObject*>* resources, |
SkTDArray<SkPDFObject*>* result); |
+ /** Destroys safely an array of SkPDFObjects. |
+ * The stack won't be used much, and stack overflow will be avoided. |
+ */ |
+ static void safeUnref(SkTDArray<SkPDFObject*>* list) { |
+ while (list->count() > 0) { |
+ SkPDFObject* obj = NULL; |
+ list->pop(&obj); |
+ if (obj) { |
+ // Releases all refs to be owned by list, unref will call destructor! |
+ if (obj->getRefCnt() == 1) { |
+ obj->transferPDFObjectsOwnership(list); |
+ } |
+ // If unref calls destructor, it won't call recursively |
+ // other ~SkPDFObjects(). |
+ obj->unref(); |
+ } |
+ } |
+ } |
+ |
protected: |
/** Subclasses must implement this method to print the object to the |
* PDF file. |
@@ -89,6 +110,13 @@ |
virtual void emitObject(SkWStream* stream, SkPDFCatalog* catalog, |
bool indirect) = 0; |
+ /** Dumps all the SkPDFObject members in the list. When the destructor is |
+ * called, the stack won't be used much. |
+ * This method will destroy the state of the object. Should be called only |
+ * during destructor calls. |
+ */ |
+ virtual void transferPDFObjectsOwnership(SkTDArray<SkPDFObject*>* list) {} |
+ |
typedef SkRefCnt INHERITED; |
}; |
@@ -111,6 +139,13 @@ |
bool indirect); |
virtual size_t getOutputSize(SkPDFCatalog* catalog, bool indirect); |
+protected: |
+ virtual void transferPDFObjectsOwnership(SkTDArray<SkPDFObject*>* list) { |
+ list->push(fObj.detach()); |
+ |
+ INHERITED::transferPDFObjectsOwnership(list); |
+ } |
+ |
private: |
SkAutoTUnref<SkPDFObject> fObj; |
@@ -406,6 +441,18 @@ |
*/ |
void clear(); |
+ virtual void transferPDFObjectsOwnership(SkTDArray<SkPDFObject*>* list) { |
+ for (int i = 0; i < fValue.count(); i++) { |
+ list->push(fValue[i].key); |
+ fValue[i].key = NULL; |
+ list->push(fValue[i].value); |
+ fValue[i].value = NULL; |
+ } |
+ fValue.reset(); |
+ |
+ INHERITED::transferPDFObjectsOwnership(list); |
+ } |
+ |
private: |
struct Rec { |
SkPDFName* key; |