469,626 Members | 883 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

best way to implement a 'static destructor'

I know two possible ways:

1. Use AppDomain.DomainUnload Event
http://msdn.microsoft.com/library/en...nloadTopic.asp
which is kinda like Turbo Pascal, which the desinger of C# made, did
it.

2. Use a hack. Make the class non-static, even though it will have no
non-static methods, declare a static field of the class itself:
static private MyClass m_instance;
Then instantiate it in the static constructor:
m_instance = new MyClass();
So, then, you can make a destructor, which should be called only when
the m_instance is on the way out, which should be after no other uses
of the class's static methods are used.

Any comments on which is better?

Zytan

Mar 23 '07 #1
4 6502
There are 2 differences in your methods.

1. ) The AppDomain.DomainUnload Event "Occurs when an AppDomain is
about to be unloaded." Your "static destructor" will be called during
the unload process.

2.) You need to have specific security permission for the first way.

But i will prefer the 1 one. simply because it isn't a HACK.

Mar 23 '07 #2
1. ) The AppDomain.DomainUnload Event "Occurs when an AppDomain is
about to be unloaded." Your "static destructor" will be called during
the unload process.
My constructor creates a StreamWriter. If I make the class non-
static, and attempt to close the StreamWriter in a finalizer, the
StreamWriter is already finalized (which I believe is wrong, since my
code is still executing, thus, C# shouldn't have begun termination of
my objects). I predict also that during DomainUnload, the same is
also true.
2.) You need to have specific security permission for the first way.
Ah, really? What is that?
But i will prefer the 1 one. simply because it isn't a HACK.
I agree.

Zytan

Mar 23 '07 #3
Zytan wrote:
>1. ) The AppDomain.DomainUnload Event "Occurs when an AppDomain is
about to be unloaded." Your "static destructor" will be called during
the unload process.

My constructor creates a StreamWriter. If I make the class non-
static, and attempt to close the StreamWriter in a finalizer, the
StreamWriter is already finalized (which I believe is wrong, since my
code is still executing, thus, C# shouldn't have begun termination of
my objects).
The finalizer is called independently of the main code. Your main code
may still be running or may have finished a long time ago.

If you have a reference to the object in your main code, the program has
to have ended already for object to be finalized.
I predict also that during DomainUnload, the same is
also true.
>2.) You need to have specific security permission for the first way.

Ah, really? What is that?
>But i will prefer the 1 one. simply because it isn't a HACK.

I agree.

Zytan

--
Göran Andersson
_____
http://www.guffa.com
Mar 23 '07 #4
My constructor creates a StreamWriter. If I make the class non-
static, and attempt to close the StreamWriter in a finalizer, the
StreamWriter is already finalized (which I believe is wrong, since my
code is still executing, thus, C# shouldn't have begun termination of
my objects).

The finalizer is called independently of the main code. Your main code
may still be running or may have finished a long time ago.
Right.
If you have a reference to the object in your main code, the program has
to have ended already for object to be finalized.
I have a data member in my class that references the object. The
finalizer in this class attemps to use it, and the object is already
finalized. (Actually, the object = StreamWriter. The object that is
finalized is the Stream object that it wraps.)

Zytan

Mar 23 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Simon Elliott | last post: by
5 posts views Thread by Simon Elliott | last post: by
13 posts views Thread by Adam H. Peterson | last post: by
2 posts views Thread by Gabrielle A. Grün | last post: by
4 posts views Thread by Morgan Cheng | last post: by
6 posts views Thread by Pallav singh | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.