TheSwamp

Code Red => .NET => Topic started by: dketels on February 24, 2023, 10:05:47 AM

Title: To Dispose or not to dispose, that's an old question?
Post by: dketels on February 24, 2023, 10:05:47 AM
Hi,

i have a question regarding Disposing an object.

I implemented a handler for the "AddHandler document.Database.ObjectAppended" event.
The signature of the method is "Private Sub HandleAppendedEvent(sender As Object, e As ObjectEventArgs)"

Just like a .net Polyline is a wrapper object and should be disposed, I should also dispose the e.DBObject?

What is your opinion?
Title: Re: To Dispose or not to dispose, that's an old question?
Post by: kdub_nz on February 24, 2023, 03:25:45 PM

This may help resolve your query.
https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-FE7D58D5-28A0-4C98-A876-D4D48F06D0B2

I don't understand the
e.DBObject  , what is this ?


Perhaps you could post your implementation.

Regards,
Title: Re: To Dispose or not to dispose, that's an old question?
Post by: gile on February 24, 2023, 04:44:39 PM
Hi,

You do not need to dispose the e.DBObject. This DBObject is already handled by the command (or whatever) which appended it to the Database.
Title: Re: To Dispose or not to dispose, that's an old question?
Post by: dketels on February 28, 2023, 02:38:42 AM
Hi, thank you for the response.

After reading your replies and doing some more research I removed the dispose of the object given in the event argument.

My reasoning is that, because I (any of my commands) did not create the object, I should not dispose of it.

Details:
In this case, it was an Xrecord that was appended to the database by autocad. In the HandleAppendEvent, I look at the object, and if it is for example a polyline I would do stuff with it.
I used to believe that, because I accessed the object, just to be sure, I needed to dispose it.
But in case of the Xrecord, me disposing it, made it unavailable to autocad, which was still busy with it. Resulting in a read/write exception and autocad crashing.