Hi all,

zyx <z...@litepdf.cz> has written on 2 March 2017 at 07:48:

> On Thu, 2017-03-02 at 00:00 +0100, Matthew Brincke wrote:
> 
> > I'm sorry that have only build-tested it a bit yet, still I'm submitting
> > the patch to PdfOutlineItem to correct memory management for review.

> Hi,
> you know, such statements sound quite discouraging. If you are not
> willing to test your own patches, then why should do it anyone else? I
> can understand it for a straightforward changes, but not for anything
> more complicated. By the way, as a bonus, writing unit test for the
> change(s) would be more than welcome.

> > Now the item given to InsertChild() is copied and the pointers are set to
> > NULL so that only that bare item is inserted, no deep copying or problems
> > with referenced items being reachable/erasable from two different trees.

> This doesn't sound correct to me, but I cannot tell yet. The attached
> patch was incomplete, caused a build break at the best.

the attached patch on this e-mail should be complete now. Then I think it's
correct, I have tested it now (test code also attached).

> > Please consider review/testing/when appropriate applying to the public
> > repository.

> No need to mention this, it's somehow understood on its own (at least
> by me, on this list).
> Bye,
> zyx

I may be away from this list for ca. 3 months for finishing my studies with
a thesis.

Best regards, mabri
--- PdfOutlines.cpp.orig	2017-02-28 21:45:40.772027080 +0000
+++ PdfOutlines.cpp	2017-03-01 21:23:04.932193655 +0000
@@ -124,11 +124,11 @@ void PdfOutlineItem::InsertChild( PdfOut
     this->InsertChildInternal( pItem, true );
 }
 
-void PdfOutlineItem::InsertChildInternal( PdfOutlineItem* pItem, bool bCheckParent )
+void PdfOutlineItem::InsertChildInternal( const PdfOutlineItem* pItem, bool bCheckParent )
 {
-    PdfOutlineItem* pItemToCheckParent = pItem;
-    PdfOutlineItem* pRoot = NULL;
-    PdfOutlineItem* pRootOfThis = NULL;
+    const PdfOutlineItem* pItemToCheckParent = pItem;
+    const PdfOutlineItem* pRoot = NULL;
+    const PdfOutlineItem* pRootOfThis = NULL;
 
     if ( !pItemToCheckParent )
         return;
@@ -162,13 +162,21 @@ void PdfOutlineItem::InsertChildInternal
             PODOFO_RAISE_ERROR( ePdfError_OutlineItemAlreadyPresent );
     }
 
+    PdfOutlineItem* pItemCopy = new PdfOutlineItem( *pItem );
+    pItemCopy->m_pParentOutline = this;
+
+    // insert only the item itself, without references to others
+    pItemCopy->SetNext( NULL );
+    pItemCopy->SetFirst( NULL );
+    pItemCopy->SetPrevious( NULL );
+
     if( m_pLast )
     {
-        m_pLast->SetNext( pItem );
-        pItem->SetPrevious( m_pLast );
+        m_pLast->SetNext( pItemCopy );
+        pItemCopy->SetPrevious( m_pLast );
     }
 
-    m_pLast = pItem;
+    m_pLast = pItemCopy;
 
     if( !m_pFirst )
         m_pFirst = m_pLast;
#include "podofo/podofo.h"

using namespace PoDoFo;

static void
outline_crash (void)
{
    PdfMemDocument doc;
    PdfMemDocument indoc1("doc1.pdf");
    PdfOutlines *inout1 = indoc1.GetOutlines();
    PdfOutlines *outout = doc.GetOutlines();
    outout->CreateRoot("doc1.pdf");
    PdfOutlineItem *root = doc.GetOutlines()->First();
//    doc.InsertPages(indoc1,0,3);
//    root->SetDestination(PdfDestination(doc.GetPage(0)));
    PdfOutlineItem *nextroot = root->Next();
    if (nextroot) nextroot->InsertChild(inout1->First());
    PdfOutlineItem *x = doc.GetOutlines()->First();
    x->Erase();
}

int main()
{
//    PdfError::EnableDebug(true); // not in 0.9.4
    try {
        outline_crash();
    }
    catch (PdfError& e) {
        e.PrintErrorMsg();
    }
    return 0;
}

#include "podofo/podofo.h"

using namespace PoDoFo;

static void
outline_crash (void)
{
    PdfMemDocument doc;
    const PdfMemDocument indoc1("doc1.pdf");
    PdfOutlines *outout = doc.GetOutlines();
    outout->CreateRoot("doc1.pdf");
    PdfOutlineItem *root = doc.GetOutlines()->First();
    doc.InsertPages(indoc1,0,3);
    root->SetDestination(PdfDestination(doc.GetPage(0)));
    PdfOutlineItem *nextroot = root->Next();
    if (nextroot) root->InsertChild(nextroot);
    PdfOutlineItem *x = doc.GetOutlines()->First();
    x->Erase();
}

int main()
{
//    PdfError::EnableDebug(true); // not in 0.9.4
    try {
        outline_crash();
    }
    catch (PdfError& e) {
        e.PrintErrorMsg();
    }
    return 0;
}

------------------------------------------------------------------------------
Announcing the Oxford Dictionaries API! The API offers world-renowned
dictionary content that is easy and intuitive to access. Sign up for an
account today to start using our lexical data to power your apps and
projects. Get started today and enter our developer competition.
http://sdm.link/oxford
_______________________________________________
Podofo-users mailing list
Podofo-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/podofo-users

Reply via email to