Author Topic: Do I need to dispose a line after it has been removed form a List<Line>?  (Read 17092 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: 6144
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