Valued Contributor
Posts: 91
Registered: ‎10-03-2011
Message 1 of 5 (386 Views)
Accepted Solution

Disposing objects.

386 Views, 4 Replies
01-07-2012 04:08 AM

Hello forum,

I'd like to know which objects I should dispose and which not (using c#):


objDatabase = MdiActiveDocument.Database;
// I do not have to dispose a reference to database of active document.


using (objTransaction = objDatabase.TransactionManager.StartTransaction())
// using statement disposes transaction.


objBlockTable = (BlockTable)objTransaction.GetObject(objDatabase.BlockTableId, OpenMode.ForRead);
// When done with objBlockTable, am I suppose to call Dispose?


objBlockTableRecord = (BlockTableRecord)objTransaction.GetObject(objDatabase.CurrentSpaceId, OpenMode.ForWrite);
// When done with objBlockTableRecord, I am suppose to call Dispose?


objBlockReference = new BlockReference(new Point3d(objPointResult.Value.X, objPointResult.Value.Y, 0), structObjectId);
// When done with objBlockReference, I am suppose to call Dispose?

Thank you.

*Expert Elite*
Posts: 8,968
Registered: ‎06-29-2007
Message 2 of 5 (382 Views)

Re: Disposing objects.

01-07-2012 04:51 AM in reply to: Ertqwa



the most simple rule is (worked well for the last years for me):

any object you create and has afterwards to be cleaned from memory has to be disposed.


Samples for .Dispose needed:

  • if you create a virtual line for calculating intersectionpoints, but you don't add this line to the database you have to free up memory by disposing it.
  • If you create a transaction object you have to free the memory from it after using it (just as additional info: the using (transaction-object) statement includes the .Dispose internally)
  • Same for DocumentLock ... if you created such an object you have to dispose it (in this case to unlock the document).


.Dispose is not needed for objects that does also exist after your code has finished

Samples for .Dispose NOT needed:

  • BlockTableRecord, LayerTableRecord Line/Circle/Arc/... geometric objects if they are appended to the database or already did exist in the database. These objects exists also after your code finished .... so you don't need to clean up the memory for these objects (because AutoCAD itself uses them)


I know this is a version of a primitive global statment, but for starting it works great:

"If objects should be alive after your code finished, don't .Dispose, it objects where just created virtually and not database resident they should be disposed"


Another help is VisualStudio in debug-mode: when you run your app in debug mode you can look at the window (within VisualStudio) where messages where send to. Because when you .Dispose transactions (or at the end of using) AutoCAD sends you the message if you forgot to dispose something.


HTH, - alfred -



Ingenieur Studio HOLLAUS ... www.hollaus.at
Valued Contributor
Posts: 67
Registered: ‎02-03-2012
Message 3 of 5 (293 Views)

Re: Disposing objects.

09-13-2012 09:14 AM in reply to: Ertqwa



Alfred or anyone how know - which window allow me to see this message of forgotten non-disposed objects?



*Expert Elite*
Posts: 8,968
Registered: ‎06-29-2007
Message 4 of 5 (281 Views)

Re: Disposing objects.

09-13-2012 10:55 AM in reply to: GrzesiekGP



I only have the German version of Visual Studio, but translated the window should have a name like "output" (while you are in debug-mode),


HTH, - alfred -

Ingenieur Studio HOLLAUS ... www.hollaus.at
Valued Mentor
Posts: 370
Registered: ‎05-06-2012
Message 5 of 5 (225 Views)

Re: Disposing objects.

09-20-2012 12:13 PM in reply to: Ertqwa

Look at the AutoDelete property of any object that derives from DisposableWrapper.


If the value of that property is false, that means the object will do nothing when it is disposed, which means that it doesn't need to be disposed.



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.