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

0 Members and 1 Guest are viewing this topic.

waterharbin

  • Guest
Hello,every.I have a List<Line>, I want to remove the first Line in the List<List>.What the difference between the two method?
method 1:
Code: [Select]
LineLst.RemoveAt(0);method 2;
Code: [Select]
Line fristLine = LineLst.First();
LineLst.Remove(firstLine);
firstLine.Dispose();

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #1 on: December 04, 2011, 03:15:48 AM »
No, while you're just dealing with list, you do not need to dispose.

You must dispose a Line (or any other DBObject) if it's newly created with 'new Line()' and not added to a Database.

See this video.
Speaking English as a French Frog

waterharbin

  • Guest
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #2 on: December 04, 2011, 05:43:43 AM »
Hello,gile.My lines in the Line<Line> are all defined with "new Line()".They are not selected form any drawings.This is my circumstances:
Code: [Select]
... //Other code
Line myLine = new Line(ptStart,ptEnd);
LineLst.Add(myLine);
... //more other code
/*I have define lots of Lines in this way,and add all of them to the LineLst.
But I didn't add them to any Database.
Now I want to remove the lines from the list one by one until the LineLst is empty.
So,I should dispose them.*/
while(LineLst.Count() > 0)
{
Line fristLine = LineLst.First(); //Obtain the first line.
LineLst.Remove(firstLine);  //Remove it form the List, so the loop can stop when the List is empty
...//Some other useful code
//At the end,the loop line need to be disposed
firstLine.Dispose();
}
I didn't add them to a Database.So, is my way right?
Or,should I just ues the code below:
Code: [Select]
LineLst.First().Dispose();
« Last Edit: December 04, 2011, 06:05:42 AM by waterharbin »

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #3 on: December 04, 2011, 06:38:25 AM »
Yes, if you do not add theme to any Database, you have to explicitly dispose them.

But, why using Line objects (derived from Entity) if you won't add any of them to a Database ?

The Autodesk.AutoCAD.Geometry namespace have a Line3dSegment class (or Line2dSegment) which are to be used for geometric calculations and do not have to be explicitly disposed.
« Last Edit: December 04, 2011, 06:44:18 AM by gile »
Speaking English as a French Frog

waterharbin

  • Guest
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #4 on: December 04, 2011, 11:10:06 PM »
Thank you,gile.I will try Line3dSegment.Thank you telling me that.I didn't know that before. And what about Point3d. Do they need to be explicitly disposed? For example:
Code: [Select]
Point3d myPoint = new Point3d(0,0,0);
« Last Edit: December 04, 2011, 11:18:34 PM by waterharbin »

SGP2012

  • Guest
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #5 on: December 05, 2011, 06:20:06 PM »
Point3d doesn't have a Dispose() method.

As a general rule, you should call Dispose() on anything that implements Dispose() for which you haven't handed over responsibility to AutoCAD. Even if its not strictly necessary, it doesn't do any harm. Another general rule (that may have a few exceptions) is that calling Dispose on something you've handed to AutoCAD won't cause any harm either.

And using the 'using' construct greatly simplifies handling of object disposal  - for example when an exception is thrown and handled in your code.

SEANT

  • Bull Frog
  • Posts: 345
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #6 on: December 05, 2011, 07:07:07 PM »
Welcome to The Swamp, Stephen.  Always nice to get input from an insider.
Sean Tessier
AutoCAD 2016 Mechanical

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #7 on: December 05, 2011, 07:15:06 PM »


Welcome to The Swamp, Stephen. Enjoy :)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Draftek

  • Guest
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #8 on: December 06, 2011, 08:06:28 AM »
It's good practice to dispose any database objects you create or open via the using statement and not wait for the garbage collector to do it for you.

Everything but the singletons like the database itself of course.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #9 on: December 06, 2011, 04:02:38 PM »
It's good practice to dispose any database objects you create or open via the using statement and not wait for the garbage collector to do it for you.

Everything but the singletons like the database itself of course.

Ahhh, Draftek was in the class..  :-)
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 #10 on: December 06, 2011, 04:53:19 PM »
Thanks for the warm welcome. I've been a member for a few weeks, but you guys keep answering everything before I get a chance. :-)

On when to dispose - sorry Draftek, I slightly disagree. If you open a DBObject in a transaction, then calling Dispose is not necessary, because AutoCAD has ownership of the object and takes care of it for you. Although - as I said before - calling Dispose on those DBObjects won't hurt (it just makes your code slightly harder to read).

In terms of DBObjects, we have that nice litle prompt sent to the VS output window to warn you if you didn't dispose of something you should have. But not all other AutoCAD objects do the same thing.

Draftek

  • Guest
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #11 on: December 07, 2011, 08:23:03 AM »
On when to dispose - sorry Draftek, I slightly disagree. If you open a DBObject in a transaction, then calling Dispose is not necessary, because AutoCAD has ownership of the object and takes care of it for you. Although - as I said before - calling Dispose on those DBObjects won't hurt (it just makes your code slightly harder to read).

That's what I thought until last week. Every DBObject that you open (including TableRecords) should be wrapped in a using statement to force a .Dispose()
According to a couple of classes I took and some conversations after.
« Last Edit: December 07, 2011, 11:13:33 AM by Draftek »

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #12 on: December 07, 2011, 09:38:27 AM »
If a object implements IDisposable then you should call dispose as soon as you are done with it.(usually unmanaged objects, database connections, streams, etc...)
 
Draftek I beleive any DBobject that a transaction is aware of will be disposed by the transaction once the transaction is disposed.
So if a DBobject is opened using Transaction.GetObject() then the transaction will dispose it assuming the transaction is disposed(wrapped in a using block, etc..)
 
With the original question of creating new lines a transaction will not be aware of it until passed in Transaction.AddNewlyCreatedDBObject ()

*******edit******
Change Objects to DBobjects
 
« Last Edit: December 07, 2011, 09:47:20 AM by Jeff H »

zoltan

  • Guest
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #13 on: December 07, 2011, 10:29:26 AM »
Another warm welcome for Stephen Preston!  It's good to have some Autodesk guys here.  Now you just need to convince Fenton and Gopinath to join (if they haven't already). :)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #14 on: December 07, 2011, 10:40:42 AM »
Quote
Stephen started his career as a scientist, and has a PhD in atomic and laser physics from the University of Oxford.

:kewl: Welcome to the swamp Stephen!!
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

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: 6150
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: 6150
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.

waterharbin

  • Guest
Re: Do I need to dispose a line after it has been removed form a List<Line>?
« Reply #30 on: December 08, 2011, 08:19:46 AM »
Wow!With so many experts here,why don't you guys write a book about .NET?Or a full comprehensive document.We would be very happy to read.Haha.