best way to implement a 'static destructor'

Z

Zytan

I know two possible ways:

1. Use AppDomain.DomainUnload Event
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemAppDomainClassDomainUnloadTopic.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
 
R

Roman Wagner

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.
 
Z

Zytan

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
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Zytan said:
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.
 
Z

Zytan

My constructor creates a StreamWriter. If I make the class non-
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top