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

Disposing objects.

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
Ertqwa
1056 Views, 4 Replies

Disposing objects.

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.

4 REPLIES 4
Message 2 of 5
Alfred.NESWADBA
in reply to: Ertqwa

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
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 5
GrzesiekGP
in reply to: Ertqwa

Hey,

 

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

 

Thanks.

Message 4 of 5

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
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 5 of 5
DiningPhilosopher
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.

 

 

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