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 31, 2019 2:08 am

Hi,

I noticed that after disposing the spire object , it seems the spire doc object is not null.

Is this correct and how do i know whether the spire doc object is not valid anymore or disposed?

Here is the sample code im using for testing:

Code: Select all
Document document = null;

try
{
   document = new Document();
   document.LoadFromFileInReadMode(tbPath.Text, Spire.Doc.FileFormat.Auto);
}
catch (Exception ex)
{
   MessageBox.Show(ex.Message);
   // Console.WriteLine(ex.Message);
}
finally
{
   try
   {
      if (document != null)
      {
         document.Dispose();
      }
   }
   catch (Exception ex)
   {
   }
}


Before dispose
Image

After dispose
Image

vernon1111
 
Posts: 44
Joined: Fri Mar 02, 2018 4:34 am

Thu Jan 31, 2019 3:45 am

Hi Vernon,

Thank you for your inquiry.
The scenario is correct. When doing disposing, the internal resources would be disposed and the process equals the instantiation of a new Document object "document = new Document()". The basic document structure still exists, so it is not null. As for how to judge whether a document has been disposed or not, you could check the memory change. After disposing, the memory would be released.
Besides, if you want to recycle the document, I'd recommend the "using" block. The document object would not exist anymore after stepping out the statement.
Code: Select all
using (Document document = new Document())
{
    document.LoadFromFileInReadMode(tbPath.Text, Spire.Doc.FileFormat.Auto);
}


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Thu Jan 31, 2019 6:20 am

Hi,

Thanks for the explanation.

Is there anyway we can know from coding if an object is valid or not instead of checking the memory drop from the task manager?

I planning to use thread with a timeout so that if a document is stuck in processing due to problem, I can stop the process.

With this method, it will be difficult for me to use using statement to release the object after processing has done.

vernon1111
 
Posts: 44
Joined: Fri Mar 02, 2018 4:34 am

Thu Jan 31, 2019 9:07 am

Hi,

Thank you for your fast response.
Like I said before, the content data would be removed when executing "document.Dispose();", only the default basic document structure exists. So, the previous document is not valid anymore to some extent. When you handle the original document after disposing, the application would throw an exception. For instance, var count = document.Sections.Count; would throw a "NullReference Exception" because the old content has been released and there's no section at present. You could check the document validation by that.
Besides, if you want the document to be null, why not directly declaring it as null after disposing?
Code: Select all
document.Dispose();
document = null;


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.Doc