• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Valued Contributor
    Ertqwa
    Posts: 73
    Registered: ‎10-03-2011
    Accepted Solution

    Disposing objects.

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


    Thank you.

    Please use plain text.
    *Expert Elite*
    Posts: 6,442
    Registered: ‎06-29-2007

    Re: Disposing objects.

    01-07-2012 04:51 AM 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
    -------------------------------------------------------------------------
    Please use plain text.
    Valued Contributor
    GrzesiekGP
    Posts: 54
    Registered: ‎02-03-2012

    Re: Disposing objects.

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

    Hey,

     

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

     

    Thanks.

    Please use plain text.
    *Expert Elite*
    Posts: 6,442
    Registered: ‎06-29-2007

    Re: Disposing objects.

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

    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
    -------------------------------------------------------------------------
    Please use plain text.
    Valued Mentor
    Posts: 306
    Registered: ‎05-06-2012

    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.

     

     

    Please use plain text.