473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 6641
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: gorda | last post by:
Hello, I have the following code, with a basic class having a constructor and destructor. in the main section, I create a static pointer to the class. My question is regarding the destructor. If...
7
by: Simon Elliott | last post by:
Consider the following, incorrect, code: #pragma hdrstop #include <condefs.h> #include <iostream> #include <conio.h> class bar {
5
by: Simon Elliott | last post by:
I have a C++ file which needs to work along these lines: class foo { private: int i1_; public: foo():i1_(12){} ~foo() {
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
2
by: Gabrielle A. Grün | last post by:
Hi All, Does anyone know a way around the illegal reference of a non-static member? Thanks. iNHERITANCE HIERACY
4
by: Morgan Cheng | last post by:
In C#, we can initialize static member variables in static constructor. Now, when to destruct these static member variables? If these variables hold external reference(e.g. log file stream), it is...
6
by: VSP | last post by:
Hello, I am just implementing singleton pattern in various ways. In one implementation I created a static member and returning that static member in the getInstance() function. I have made...
6
by: Pallav singh | last post by:
Hi All How Does compiler implement virtual destructor ??? as we all know if base class destructor is virtual..........then while wriiting statemnt like this Base * b = new Derived( );...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.