TheSwamp

Code Red => .NET => Topic started by: kdub_nz on July 05, 2022, 10:28:59 PM

Title: Disposing of DocumentLock Object
Post by: kdub_nz on July 05, 2022, 10:28:59 PM
It's my understanding that LockDocument method requires a matching call to Dispose on the DocumentLock Object.

Usually this is achieved by the use of a using statement :
Code - C#: [Select]
  1. using (DocumentLock _documentLock = doc.LockDocument())
  2.     {
  3.     ;; code block..
  4.     )

The
Code: [Select]
using(Transaction ... ) is then started inside the code block.

Once the using statement for the DocumentLock ends the database is unlocked

similar to the 
Code: [Select]
using (Transaction ... )
 

Does the
Code: [Select]
doc.CloseAndDiscard();    call negate the requirement to Dispose the  DocumentLock Object. ??

Regards,
Title: Re: Disposing of DocumentLock Object
Post by: It's Alive! on July 06, 2022, 06:19:38 AM
I don’t think DocumentLock actually locks the database, I think it just prevents execution contexts
It’s not an AcDbObject, so I’m pretty sure that letting the garbage collector handle it is safe, Dispose only calls its destructor

BTW, doc.CloseAndDiscard(); calls the COM version of Document.close with the false parameter.

Also an FYI "The document does not need to be locked to open an AcDbObject in AcDb::kForRead, nor to get system variables."
according to the docs
Title: Re: Disposing of DocumentLock Object
Post by: kdub_nz on July 06, 2022, 05:40:21 PM
Thanks Daniel, that's something to chew on.

The code I was looking at was changing Layer names in a side doc.
It just had 

Code - C#: [Select]
  1. Document doc = Application.DocumentManager.Open(dwgPath);
  2. ;; etc
  3. doc.LockDocument();
  4. try
  5. {
  6.     using (Transaction ... ...
  7.     {
  8.          ;;code block to find and change layer names
  9.          tr.Commit();
  10.      }
  11.      db.SaveAs(dwgPath, DwgVersion.Current);
  12.      doc.CloseAndDiscard();
  13. }
  14. catch(
  15. ;;; etc
  16. }
  17.  
  18.  

It was being called from a Form, iterating over a list of selected drawings.
. . . a little buzzer went off in my brain when I saw the LockDocument() was not disposed.

Stay well !