List Info

Thread: C-Sharp (C#) Group: Is the Class or object destructor guaranteed to be called even in the case of an




C-Sharp (C#) Group: Is the Class or object destructor guaranteed to be called even in the case of an
user name
2006-07-25 18:10:52
Im trying to understand something about the inner workings
of C# and
.Net with respect to destructors. I am toying with the idea
of having
the destructor close and write a stream to a file (leaving
it open
until either the object exhausts all references to itself,
or an
abnormal abort). So that is really the core of my question,
will an
abnormal abort call all destructors as it is aborting?

Thanks for the help in understanding this
-SteveM


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

C-Sharp (C#) Group: Re: Is the Class or object destructor guaranteed to be called even in the case o
user name
2006-07-25 21:24:43
A destructor should only ever be used as a backup to close unmanaged resources. An unmanaged exception will not call the destructor. The destructor is called by the Garbage Collector when it needs to free up resources which means it is not predictable. The best option for dealing with this is three fold;

1. Implement a lazy initaliser for any unmanaged resources
2. Close any unmanaged resources when you are internally finished working with them (EG method called that opens a file stream, close it at the end of the method)
3. Implement IDisposable as a backup. There is a discussion of best practices with this over at http://www.codeproject.com/csharp/IDispose.asp

Liam

On 7/25/06, SteveM <philips.com">steve.moodyphilips.com> wrote:

Im trying to understand something about the inner workings of C# and
.Net with respect to destructors. I am toying with the idea of having
the destructor close and write a stream to a file (leaving it open
until either the object exhausts all references to itself, or an
abnormal abort). So that is really the core of my question, will an
abnormal abort call all destructors as it is aborting?

Thanks for the help in understanding this
-SteveM



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

C-Sharp (C#) Group: Re: Is the Class or object destructor guaranteed to be called even in the case o
user name
2006-07-26 07:23:27
One more thing.
If u use an idisposable object allways use the using
construct. Example

using(IDataReader reader = db.ExecuteReader(dbCommand)) {
... }

This will call the dispose method of reader object in any
case the
focus leavesthis code block. And remember ther are more
classes that
implement idisposable than u think.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )