Disposing objects.

Disposing objects.

Ertqwa
Advocate Advocate
1,764 Views
4 Replies
Message 1 of 5

Disposing objects.

Ertqwa
Advocate
Advocate

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);
...
objBlockTableRecord.AppendEntity(objBlockReference);
// When done with objBlockReference, I am suppose to call Dispose?


Thank you.

0 Likes
Accepted solutions (1)
1,765 Views
4 Replies
Replies (4)
Message 2 of 5

Alfred.NESWADBA
Consultant
Consultant
Accepted solution

Hi,

 

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 -

 

 

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
Message 3 of 5

Anonymous
Not applicable

Hey,

 

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

 

Thanks.

0 Likes
Message 4 of 5

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

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 -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 5 of 5

DiningPhilosopher
Collaborator
Collaborator

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.

 

 

0 Likes