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.
Solved! Go to Solution.
Solved by Alfred.NESWADBA. Go to 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:
.Dispose is not needed for objects that does also exist after your code has finished
Samples for .Dispose NOT needed:
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 -
Hey,
Alfred or anyone how know - which window allow me to see this message of forgotten non-disposed objects?
Thanks.
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 -
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.