.NET

Reply
Distinguished Contributor
cgay
Posts: 125
Registered: ‎08-02-2005
Message 1 of 3 (74 Views)

Document.Dispose Problems

74 Views, 2 Replies
02-28-2006 01:10 PM
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
*Tony Tanzillo
Message 2 of 3 (74 Views)

Re: Document.Dispose Problems

03-03-2006 08:57 AM 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
Distinguished Contributor
cgay
Posts: 125
Registered: ‎08-02-2005
Message 3 of 3 (74 Views)

Re: Document.Dispose Problems

03-03-2006 02:45 PM 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
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.