Author Topic: Do I need to dispose a line after it has been removed form a List<Line>?  (Read 17088 times)

0 Members and 1 Guest are viewing this topic.

Draftek

  • Guest
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #15 on: December 07, 2011, 11:06:20 AM »
Stephen, I was told by Gopi that he had recently been informed by Albert that relying on the transaction to handle the dispose() was not a good idea.

The reason was for some complex objects such as solids there may be some underlying ties to the database even after the transaction has closed it. Since the Garbage Collector runs in a separate thread and runs who knows when this has caused errant crashes of AutoCAD.

Since we are not sure which objects have this hook, he said to be safe just wrap them all in a using block.

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #16 on: December 07, 2011, 11:12:46 AM »
Wow!
I had no idea that was Stephen Preston.
 
I hope to see more of your posts in the future.

SGP2012

  • Guest
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #17 on: December 07, 2011, 12:57:21 PM »
Draftek - Who told you that?

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #18 on: December 07, 2011, 01:22:10 PM »
Stephen, it was Gopinath Taget in his class on current .NET design patterns.
Revit 2019, AMEP 2019 64bit Win 10

SGP2012

  • Guest
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #19 on: December 07, 2011, 02:04:24 PM »
Hi guys,

I work in the same office as both Gopinath andf Albert Szilvasy (Albert being the architect for the AutoCAD .NET API), so I was able to quickly sort this out. Unfortunately, Gopinath misunderstood an answer Albert gave when he quizzed him on this, and so made a mistake with the advice he gave in his AU class. Sorry about that. Here is the correct information - straight from the brain of Albert ...

You don't have to Dispose of a DBObject that was already part of the DWG database and which you open in a Transaction. An example of this is:

Code - C#: [Select]
  1.         [CommandMethod("ChgLayerNames")]
  2.         public static void ChangeLayerNames()
  3.         {
  4.             Database db = Application.DocumentManager.MdiActiveDocument.Database;
  5.  
  6.             using (Transaction tr = db.TransactionManager.StartTransaction())
  7.             {
  8.                 LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
  9.  
  10.                 foreach (ObjectId ltrId in lt)
  11.                 {
  12.                     //No need to have using statement for the ltr's we open. No need to call Dispose()
  13.                     LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(ltrId, OpenMode.ForRead);
  14.  
  15.                     if (ltr.Name != "0")
  16.                     {
  17.                         ltr.UpgradeOpen();
  18.                         ltr.Name = "First Floor " + ltr.Name;
  19.                     }
  20.                 }
  21.  
  22.                 tr.Commit();
  23.             }
  24.         }
  25.  

You can use a using statement in the above if you really want to, but it just makes your code harder to read.

You should use a using statement if you create a DBObject you intend to add to a Transaction. This is because you can't assume that an exception won't be thrown before you add the DBObject to the Transaction once the object is added to the transaction, its safe to leave AutoCAD to Dispose of the .NET wrapper for the object you added).

Code - C#: [Select]
  1.         [CommandMethod("AddLyr")]
  2.         public static void AddLayer()
  3.         {
  4.             Database db = Application.DocumentManager.MdiActiveDocument.Database;
  5.  
  6.             using (Transaction tr = db.TransactionManager.StartTransaction())
  7.             {
  8.                 LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
  9.  
  10.                 LayerTableRecord ltr;
  11.                 //Use a using stament here because an exception may be thrown before we can
  12.                 // add the DBObject to the Transaction.
  13.                 using (ltr = new LayerTableRecord())
  14.                 {
  15.                     ltr.Name = "Test";
  16.                     lt.Add(ltr);
  17.                     tr.AddNewlyCreatedDBObject(ltr, true);
  18.                 }
  19.  
  20.                 tr.Commit();
  21.             }
  22.         }
  23.  

Sorry again for this misunderstanding. I hope you've not all spent time going back and adding redundant 'using' satetments to your codebase.



edit->kdub code = csharp formatting
« Last Edit: July 30, 2012, 09:33:00 PM by Kerry »

Draftek

  • Guest
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #20 on: December 07, 2011, 02:15:48 PM »
No worries at all and thanks for the info.

I was starting to wonder if I heard it wrong, much like when my wife is giving me some instructions...

gopinatht

  • Guest
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #21 on: December 07, 2011, 02:23:09 PM »
Hi Guys,

Stephen is correct. You dont have to use the "using" statement with existing DB resident objects that are opened with the transaction (provided the transaction object is created with the "using" statement).

I originally thought that we need to explicitly dispose off existing DB resident objects as well that are opened with a transaction but because the transaction will be disposed automatically in the UI thread in case of an exception (provided you use the "using" statement with the transaction object), the DB objects openeds with the transaction will also be disposed off properly.

To simplify:

1) Always use the "using" statement with the transaction object
2) Always use the "using" statement with newly created DB Objects being added to the transaction
3) Always using the "using" statement with DB Objects that are not added to the database
4) You dont have to use the "using" statement with existing DB Objects opened with a transaction object

Cheers
Gopinath

Draftek

  • Guest
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #22 on: December 07, 2011, 02:33:20 PM »
Thanks Gopinath.

Draftek

  • Guest
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #23 on: December 07, 2011, 02:33:58 PM »
Stephen, it was Gopinath Taget in his class on current .NET design patterns.

Where were you sitting?

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #24 on: December 07, 2011, 03:24:07 PM »
Thanks for contributing, guys.  When and how to properly dispose is one of the more nebulous topics, particularly among those who are not dedicated programmers.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #25 on: December 07, 2011, 03:46:24 PM »
Waterharbin,
Could you please start more threads.
 
Thanks again Gopinath and Stephen.
 

zoltan

  • Guest
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #26 on: December 07, 2011, 04:04:13 PM »
Wow. I was in that class too! We should have had a Web-Meet!  :lmao:

waterharbin

  • Guest
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #27 on: December 08, 2011, 01:34:43 AM »
Oh,my holy God!! I have never expected my topic to be so hot!!!!Thank you all.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #28 on: December 08, 2011, 08:05:09 AM »
Stephen, it was Gopinath Taget in his class on current .NET design patterns.

Where were you sitting?

If you were standing at the back of the room, I was in the first seat on the left hand side.

I felt like standing up in every class and saying, Who here is a member of "The Swamp" but never did it.
« Last Edit: December 08, 2011, 08:11:34 AM by MexicanCustard »
Revit 2019, AMEP 2019 64bit Win 10

zoltan

  • Guest
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #29 on: December 08, 2011, 08:15:55 AM »
We should really have a meet-up next year and get some alligator stickers with our handles made to put on to our badges.  If I remember next November I will try to make it happen.