Using for LayerTableRecord(LTR)

Using for LayerTableRecord(LTR)

Anonymous
Not applicable
1,293 Views
9 Replies
Message 1 of 10

Using for LayerTableRecord(LTR)

Anonymous
Not applicable

Please take a look athe C# sample here

 

The LTR gets disposed before the transaction is commited and is that correct? I have not tested the code myself as I have only Inventore development setup.

 

Not sure if the DBObject.Dispose actually disposes the resources or decreses the ref count..

0 Likes
Accepted solutions (1)
1,294 Views
9 Replies
Replies (9)
Message 2 of 10

_gile
Consultant
Consultant

Hi,

 

Yes, it's correct.

Wrapping newly created DBObjects in a using statement, is a practice to insure the DBObject will be disposed in case an error occurs before the DBObject is added to the Transaction*, as soon as the DBObject is added to the Transaction, disposing the Transaction will take care of the disposing the DBObject.

 

* IMO, this is useless in this sample (none error can occur). The Test1() and Test2() methods in this recent reply shows this.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 10

Anonymous
Not applicable

Yeah i get it the reference count for the DBObject doesnt reach zero until after the transaction goes out of scope.

 

so what do you do if you have to create say 10 dbobjects to construct something to ensure the stack rolls back?

 

using (var p1=new Line())

using(var c1=new Line))

...

using(var o10=new Entity())

{

}

 

?

0 Likes
Message 4 of 10

_gile
Consultant
Consultant

As I try to explain, if an error or user break may occur after a DBObject is created and before it is added to the transaction, do use a using statement, if none error can occur (as in the sample you linked), the using statement is not mandatory, and this, whatever the number of DBObject you create.

 

PS: I forgot the link in my last message, it would have been:

* IMO, this is useless in this sample (none error can occur). The Test1() and Test2() methods in this recent reply shows this.

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 10

Anonymous
Not applicable

OK, the confusion, to me, was that I am using 2 different docos 1 from AutoCAD and the other from BricsCAD.

 

DBObject is derived from Drawable in AC and DisposableWrapper in BricsCAD which implements IDisposable. 'using' construct also calls the dispose meth of the resource created in the header IF it has one, however in AC there is no Dispose meth for DBobjects nor for its derivatives. that's is the confusion for me and wondering about ref counts!

 

sorry mate!

0 Likes
Message 6 of 10

_gile
Consultant
Consultant

In AutoCAD, Drawable is derived from RXObject which is derived from DisposableWrapper, so, DBObject and derived classes do have a Dispose() method.

You can read this topic at TheSwamp and pay attention to Tony "theMaster" Tanzillo advices.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 10

Anonymous
Not applicable

Thanks for that!

 

0 Likes
Message 8 of 10

Anonymous
Not applicable

When we write managed code, we are always relying on the GC to clean up the managed objects we create and use (whether they implement IDisposable or not), and we do not need to be concerned with deallocating those objects or the memory they consume, calling d'tors, etc.

The comment on GC from the master is inaccurate.

Actually, GC only frees the resources that have reached the zero count. objects implementing IDisposable have to release their own resources and even then when they are released (through Dispose meth) only the ref count is descresed on that reference and ultimately the GC that actually frees the resources.

 

the Test2 in the code you cited uses Erase method to mark it, not releasing the resource. when Circle was created with the new operator its ref count would have been bumped up and everytime it is on the right side of the assignment operator.

 

what is not clear to me is whether the transaction object adds ref count or not when the Add() meth is called and does it release the ref count for Erase() marked objects. in C++ when a COM interface passed to a func a ref is added?

One way to find that out is by allocating a large number of objects and then testing with diag tools.

0 Likes
Message 9 of 10

_gile
Consultant
Consultant
Accepted solution

@Anonymous wrote:

the Test2 in the code you cited uses Erase method to mark it, not releasing the resource. when Circle was created with the new operator its ref count would have been bumped up and everytime it is on the right side of the assignment operator.


Releasing the resources of the Circle has nothing to do with the Erase() functions the Circle is disposed when the Transaction is disposed because it have been added to the Transaction with the AddNewlyCreatedDBObject() method. This way, the circle is treated as any DBObject opened with the Transaction.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 10 of 10

Anonymous
Not applicable

according to the doco the Erase method sets the erase bit, so is it for some other purpose?

>> AddNewlyCreatedDBObject() method. This way, the circle is treated as any DBObject opened with the Transaction.

I see what you mean, thanks!!

0 Likes