.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Document.Dispose Problems

2 REPLIES 2
Reply
Message 1 of 3
cgay
358 Views, 2 Replies

Document.Dispose Problems

Hello everyone,

I have some simple code that shows my problem.
I am using VB.NET 2005, AutoCAD 2006.

[code]
Try
Dim aDoc as Autodesk.AutoCAD.ApplicationServices.Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
aDoc.Editor.WriteMessage(aDoc.Name)
aDoc.Dispose
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
[/code]

Ok, simple enough. The problem occurs the second time this code is run. Apparently, when aDoc.Dispose is called, this completely destroys its managed reference. But if I try to assign it from MDIActiveDoc again, it is still destroyed. Can anyone explain why this happens? The fix is simple, never call .Dispose on a variable that is created from MDIActiveDoc, but I hope someone can explain why this is. I thought that if I called dispose, I was just disposing of my instance, and that a subsequent call to MDIActiveDoc would return the ActiveDoc object again.

Thanks,
C
2 REPLIES 2
Message 2 of 3
Anonymous
in reply to: cgay

The DocumentManager class stores a list of Document instances
in a hashtable. When you ask for a Document object, it first checks
the hashtable to see if there is already one for the associated
document. If not, it creates a new Document and adds it to the
hashtable and returns that.

When you call Dispose on the Document object, the instance is
disposed, but it is not removed from the hashtable, so when you
ask for the instance for the same document again, you get the
same (now disposed) instance.

In short, you should probably never call Dispose() on Document
objects, or most others that are aggregates, collection members,
and/or not directly creatable via a constructor.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

wrote in message news:5098257@discussion.autodesk.com...
Hello everyone,

I have some simple code that shows my problem.
I am using VB.NET 2005, AutoCAD 2006.

[code]
Try
Dim aDoc as Autodesk.AutoCAD.ApplicationServices.Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
aDoc.Editor.WriteMessage(aDoc.Name)
aDoc.Dispose
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
[/code]

Ok, simple enough. The problem occurs the second time this code is run. Apparently, when aDoc.Dispose is called, this completely destroys its managed reference. But if I try to assign it from MDIActiveDoc again, it is still destroyed. Can anyone explain why this happens? The fix is simple, never call .Dispose on a variable that is created from MDIActiveDoc, but I hope someone can explain why this is. I thought that if I called dispose, I was just disposing of my instance, and that a subsequent call to MDIActiveDoc would return the ActiveDoc object again.

Thanks,
C
Message 3 of 3
cgay
in reply to: cgay

Tony,

Thank you, that explains everything. I forgot that the Document manager is a collection of documents (DocumentCollection object).

Thanks,
C

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost