Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Jan 23, 2020 1:26 pm

Hi,

I'm using something like this:

Spire.Doc.Document documentA = new Spire.Doc.Document();

documentA.LoadFromFile("My file name");

Spire.Doc.Document documentB = documentA.Clone();

documentA.Dispose();

documentB.Clone();

At the last line, documentB.Clone(), an exception is throwned.

If I do not dispose of the documentA, everything is ok.

However, in my perspective, for the sanitary of my application and the large number of documents that I have to produce, I should really dispose document each time I finished using them.

I like to just have to clone the document instead of having to reload it from IO but I must have to dispose of the original document.

What am I missing?

Here is the stack trace from Spire.doc component:

at Spire.Doc.Formatting.FormatBase.ᜂ(FormatBase A_0)
at Spire.Doc.Formatting.FormatBase.ImportContainer(FormatBase format)
at Spire.Doc.Documents.Style.CloneImpl()
at sprᮽ.ᜋ()
at sprᮽ.ᜊ()
at Spire.Doc.Documents.Style.ᜀ(IStyle A_0, Document A_1)
at Spire.Doc.Documents.Style.CloneRelationsTo(Document doc, OwnerHolder nextOwner)
at Spire.Doc.Collections.StyleCollection.Add(IStyle style)
at Spire.Doc.Documents.Style.ᜁ(Document A_0, Document A_1)
at Spire.Doc.Table.ᜀ(Document A_0, Document A_1)
at Spire.Doc.Table.CloneRelationsTo(Document doc, OwnerHolder nextOwner)
at Spire.Doc.DocumentObject.CloneRelationsTo(Document doc, OwnerHolder nextOwner)
at Spire.Doc.Body.CloneRelationsTo(Document doc, OwnerHolder nextOwner)
at Spire.Doc.DocumentObject.CloneRelationsTo(Document doc, OwnerHolder nextOwner)
at Spire.Doc.Section.CloneRelationsTo(Document doc, OwnerHolder nextOwner)
at Spire.Doc.Collections.DocumentObjectCollection.OnInsert(Int32 index, DocumentObject entity)
at Spire.Doc.Collections.DocumentObjectCollection.Add(IDocumentObject entity)
at Spire.Doc.Collections.DocumentObjectCollection.ᜀ(DocumentObjectCollection A_0)
at Spire.Doc.Document.ImportContent(IDocument doc, Boolean importStyles)
at Spire.Doc.Document..ctor(Document doc)
at Spire.Doc.Document.CloneImpl()
at Spire.Doc.Document.Clone()

Thanks

EDIT:
After doing a couple of tests, I've figured it out the problem occurs when I have a page footer. At least, it's one case.
I've created a new document which I then add a page footer and then BOOM.
I've added the document as an attachment.
But it's not the same exception as my original post.

Here is the new exception:
System.NullReferenceException: Object reference not set to an instance of an object.
at Spire.Doc.Documents.Paragraph.ᜀ(Document A_0, Document A_1)
at Spire.Doc.Documents.Paragraph.ᜂ(Document A_0, Document A_1)
at Spire.Doc.Documents.Paragraph.CloneRelationsTo(Document doc, OwnerHolder nextOwner)
at Spire.Doc.Collections.DocumentObjectCollection.OnInsert(Int32 index, DocumentObject entity)
at Spire.Doc.Collections.DocumentObjectCollection.Add(IDocumentObject entity)
at Spire.Doc.Collections.DocumentObjectCollection.ᜀ(DocumentObjectCollection A_0)
at Spire.Doc.Body.CloneImpl()
at Spire.Doc.Documents.SDTContent.CloneImpl()
at Spire.Doc.Documents.StructureDocumentTag.CloneImpl()
at Spire.Doc.Collections.DocumentObjectCollection.ᜀ(DocumentObjectCollection A_0)
at Spire.Doc.Body.CloneImpl()
at Spire.Doc.HeadersFooters.CloneImpl()
at Spire.Doc.Section.CloneImpl()
at Spire.Doc.Collections.DocumentObjectCollection.ᜀ(DocumentObjectCollection A_0)
at Spire.Doc.Document.ImportContent(IDocument doc, Boolean importStyles)
at Spire.Doc.Document..ctor(Document doc)
at Spire.Doc.Document.CloneImpl()
at Spire.Doc.Document.Clone()

mystcreater
 
Posts: 18
Joined: Tue Sep 29, 2015 12:10 am

Fri Jan 24, 2020 6:22 am

Hello,

Thanks for your inquiry.
As for the first issue, generally, the dispose method should be called only after all the relevant operations of the document are finished, otherwise it may cause some unexpected errors. I suggest you use the following modified code instead.
Code: Select all
   
    Spire.Doc.Document documentA = new Spire.Doc.Document();
    documentA.LoadFromFile("file name");
    Spire.Doc.Document documentB = documentA.Clone();
    documentB.Clone();
    documentA.Dispose();

As for the second issue, I did reproduce your issue with the code you used, but it could work well with the code above.
If there is any question, please feel free to contact us.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Doc