[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

Why is there no Static Destructor?

Rodney S. Foley

10/26/2002 10:39:00 PM

Why would Microsoft allow a Static Constructor, but not allow a Static
Destructor?

If you construct something, wouldn't it be nice to be able to destruct it
regardless of if it was constructed statically?? Especially if you are
dealing with unmanaged items in a static constructor, it would be nice to
tell the unmanaged code that you are done with it's stuff. :) So what
happened to Classes that did some static stuff with unmanaged code in a
static constructor when the application is closed? The unmanaged code seems
to be leaving this stuff out there. Not allow a static destructor seems to
allow for memory leaks possibilities. Is there another solution that can do
the same thing as a static destructor that would ONLY be called
AUTOMATICALLY once the application is closed.

Anyone at Microsoft have a "Good" reason why it was left out? And do they
plan on adding it to a future release?


13 Answers

Willem Kokke

10/26/2002 11:50:00 PM

0

I'm not sure why they're not there, but there's a easy solution..

never aquire unmanaged resources directly in a static constructor.

if you simply follow the rule that all unmanaged resources should be
aquiered and released in a class, that implements dispose and a finalizer
that calls dispose if it isn't called yet, the need for static destructors
is gone..

instead of aquiering the unmanaged resources, you simply instatiate the
managed class, which will take care of releasing the resources themselves
through the finalizer..

HTH,
Willem

"Rodney S. Foley" <aalst@aalst.com> wrote in message
news:#86CsfTfCHA.2440@tkmsftngp08...
> Why would Microsoft allow a Static Constructor, but not allow a Static
> Destructor?
>
> If you construct something, wouldn't it be nice to be able to destruct it
> regardless of if it was constructed statically?? Especially if you are
> dealing with unmanaged items in a static constructor, it would be nice to
> tell the unmanaged code that you are done with it's stuff. :) So what
> happened to Classes that did some static stuff with unmanaged code in a
> static constructor when the application is closed? The unmanaged code
seems
> to be leaving this stuff out there. Not allow a static destructor seems
to
> allow for memory leaks possibilities. Is there another solution that can
do
> the same thing as a static destructor that would ONLY be called
> AUTOMATICALLY once the application is closed.
>
> Anyone at Microsoft have a "Good" reason why it was left out? And do they
> plan on adding it to a future release?
>


Rodney S. Foley

10/27/2002 12:18:00 AM

0

Easy to say not easy to do.

There are reasons for static constructors, and sometimes you have no choice
but to access unmanaged code in them. I am in this situation. So telling
me not to do something I have no choice to do is not helpful. I should not
have to explain my situation, since my request provide all the information
needed, but I will.

My class does not get instantiated, it is full of nothing but static methods
that wrap unmanaged C functions. There is stuff that has to be done before
calling one of those static methods, so this has to be done in a static
constructor. This is one of the purposes of a static constructor. If I
could have done this stuff in a normal constructor I would be doing this.

If Microsoft would provide a 'logical' way for me to follow the rule you
specified, then I would be doing it that way, and I would never had posted
anything about this.

Thanks,

Rodney


"Willem Kokke" <w.kokke@dion-software.com> wrote in message
news:esfruFUfCHA.1816@tkmsftngp12...
> I'm not sure why they're not there, but there's a easy solution..
>
> never aquire unmanaged resources directly in a static constructor.
>
> if you simply follow the rule that all unmanaged resources should be
> aquiered and released in a class, that implements dispose and a finalizer
> that calls dispose if it isn't called yet, the need for static destructors
> is gone..
>
> instead of aquiering the unmanaged resources, you simply instatiate the
> managed class, which will take care of releasing the resources themselves
> through the finalizer..
>
> HTH,
> Willem
>
> "Rodney S. Foley" <aalst@aalst.com> wrote in message
> news:#86CsfTfCHA.2440@tkmsftngp08...
> > Why would Microsoft allow a Static Constructor, but not allow a Static
> > Destructor?
> >
> > If you construct something, wouldn't it be nice to be able to destruct
it
> > regardless of if it was constructed statically?? Especially if you are
> > dealing with unmanaged items in a static constructor, it would be nice
to
> > tell the unmanaged code that you are done with it's stuff. :) So what
> > happened to Classes that did some static stuff with unmanaged code in a
> > static constructor when the application is closed? The unmanaged code
> seems
> > to be leaving this stuff out there. Not allow a static destructor seems
> to
> > allow for memory leaks possibilities. Is there another solution that
can
> do
> > the same thing as a static destructor that would ONLY be called
> > AUTOMATICALLY once the application is closed.
> >
> > Anyone at Microsoft have a "Good" reason why it was left out? And do
they
> > plan on adding it to a future release?
> >
>
>


Willem Kokke

10/27/2002 1:03:00 AM

0

then you still didn't get my point.
ms-help://MS.VSCC/MS.MSDNVS/cpguide/html/cpconimplementingdisposemethod.htm

By implementing the standard disposing pattern like this, you guarantee that
the unmanaged resources are released sometime after the last reference went
out of scope.

public class UnmanagedWrapper
{
public UnmamagedWrapper()
{
AllocateUnmanagedResources();
}

public void Dispose()
{
Dispose(true);
}

private Dispode(bool disposing)
{
if (disposing)
{
GC.SuppressFinalize(this);
}
ReleaseUnmanagedResources();
}

~UnmanagedWrapper()
{
Dispose(false);
}
}

now if you have a static constructor like this, you guarantee that the
unmanaged resources are there before the first static method gets invoked,
and that they will be deleted at the programs end (since the finalizer of
the UnmanagedResources class will be called then

public class StaticClass
{
private static myUnmanagedResources;

static StaticClass()
{
myUnmanagedResources = new UnmanagedResources();
}
}

I agree that a static destructor would be a logical feature, there is none,
propably for some good reason.. this is an easy way to solve the problem
nontheless

HTH,
Willem

in that static constru
"Rodney S. Foley" <aalst@aalst.com> wrote in message
news:OVr1AXUfCHA.4228@tkmsftngp08...
> Easy to say not easy to do.
>
> There are reasons for static constructors, and sometimes you have no
choice
> but to access unmanaged code in them. I am in this situation. So telling
> me not to do something I have no choice to do is not helpful. I should
not
> have to explain my situation, since my request provide all the information
> needed, but I will.
>
> My class does not get instantiated, it is full of nothing but static
methods
> that wrap unmanaged C functions. There is stuff that has to be done
before
> calling one of those static methods, so this has to be done in a static
> constructor. This is one of the purposes of a static constructor. If I
> could have done this stuff in a normal constructor I would be doing this.
>
> If Microsoft would provide a 'logical' way for me to follow the rule you
> specified, then I would be doing it that way, and I would never had posted
> anything about this.
>
> Thanks,
>
> Rodney
>
>
> "Willem Kokke" <w.kokke@dion-software.com> wrote in message
> news:esfruFUfCHA.1816@tkmsftngp12...
> > I'm not sure why they're not there, but there's a easy solution..
> >
> > never aquire unmanaged resources directly in a static constructor.
> >
> > if you simply follow the rule that all unmanaged resources should be
> > aquiered and released in a class, that implements dispose and a
finalizer
> > that calls dispose if it isn't called yet, the need for static
destructors
> > is gone..
> >
> > instead of aquiering the unmanaged resources, you simply instatiate the
> > managed class, which will take care of releasing the resources
themselves
> > through the finalizer..
> >
> > HTH,
> > Willem
> >
> > "Rodney S. Foley" <aalst@aalst.com> wrote in message
> > news:#86CsfTfCHA.2440@tkmsftngp08...
> > > Why would Microsoft allow a Static Constructor, but not allow a Static
> > > Destructor?
> > >
> > > If you construct something, wouldn't it be nice to be able to destruct
> it
> > > regardless of if it was constructed statically?? Especially if you
are
> > > dealing with unmanaged items in a static constructor, it would be nice
> to
> > > tell the unmanaged code that you are done with it's stuff. :) So
what
> > > happened to Classes that did some static stuff with unmanaged code in
a
> > > static constructor when the application is closed? The unmanaged code
> > seems
> > > to be leaving this stuff out there. Not allow a static destructor
seems
> > to
> > > allow for memory leaks possibilities. Is there another solution that
> can
> > do
> > > the same thing as a static destructor that would ONLY be called
> > > AUTOMATICALLY once the application is closed.
> > >
> > > Anyone at Microsoft have a "Good" reason why it was left out? And do
> they
> > > plan on adding it to a future release?
> > >
> >
> >
>
>


Chris R

10/27/2002 2:50:00 AM

0

How to write a static class that needs to manage resources, using
SqlConnection as an example. Not the best example, but it works.

This is a tightly-woven version compared to the IDisposable option,
putting everything into one class instead of two and keeping the private
instance of the class alive as long as the static class is alive. There
are yet other variations along these themes, but any time that you need
to manage resourses, you will need to use some form of instantiation to
guarante proper disposal.

***********************************
public class NeedsManagedClass
{
//*** include a private static field to this class.***.
private static NeedsManagedClass tc;

private static System.Data.SqlClient.SqlConnection sqlConnection1;

//*** Add a private constructor ***
//*** so it can't be instantiated elsewhere.***
private NeedsManagedClass()
{
sqlConnection1 = new System.Data.SqlClient.SqlConnection();
sqlConnection1.ConnectionString = "<connection string>"
}

static NeedsManagedClass()
{
//*** instantiate the private constructor.***
tc = new NeedsManagedClass();
}

// write your static methods.
public static void Open()
{
sqlConnection1.Open();
}
public static void Close()
{
sqlConnection1.Close();
sqlConnection1 = null;
}

//*** This will run when the application is terminated.***
~NeedsManagedClass()
{
if( sqlConnection1 != null )
sqlConnection1.Close();
}
}
***********************************

Chris R.

"Rodney S. Foley" <aalst@aalst.com> wrote in message
news:OVr1AXUfCHA.4228@tkmsftngp08...
> Easy to say not easy to do.
>
> There are reasons for static constructors, and sometimes you have no
choice
> but to access unmanaged code in them. I am in this situation. So
telling
> me not to do something I have no choice to do is not helpful. I
should not
> have to explain my situation, since my request provide all the
information
> needed, but I will.
>
> My class does not get instantiated, it is full of nothing but static
methods
> that wrap unmanaged C functions. There is stuff that has to be done
before
> calling one of those static methods, so this has to be done in a
static
> constructor. This is one of the purposes of a static constructor. If
I
> could have done this stuff in a normal constructor I would be doing
this.
>
> If Microsoft would provide a 'logical' way for me to follow the rule
you
> specified, then I would be doing it that way, and I would never had
posted
> anything about this.
>
> Thanks,
>
> Rodney
>
>
> "Willem Kokke" <w.kokke@dion-software.com> wrote in message
> news:esfruFUfCHA.1816@tkmsftngp12...
> > I'm not sure why they're not there, but there's a easy solution..
> >
> > never aquire unmanaged resources directly in a static constructor.
> >
> > if you simply follow the rule that all unmanaged resources should be
> > aquiered and released in a class, that implements dispose and a
finalizer
> > that calls dispose if it isn't called yet, the need for static
destructors
> > is gone..
> >
> > instead of aquiering the unmanaged resources, you simply instatiate
the
> > managed class, which will take care of releasing the resources
themselves
> > through the finalizer..
> >
> > HTH,
> > Willem
> >
> > "Rodney S. Foley" <aalst@aalst.com> wrote in message
> > news:#86CsfTfCHA.2440@tkmsftngp08...
> > > Why would Microsoft allow a Static Constructor, but not allow a
Static
> > > Destructor?
> > >
> > > If you construct something, wouldn't it be nice to be able to
destruct
> it
> > > regardless of if it was constructed statically?? Especially if
you are
> > > dealing with unmanaged items in a static constructor, it would be
nice
> to
> > > tell the unmanaged code that you are done with it's stuff. :) So
what
> > > happened to Classes that did some static stuff with unmanaged code
in a
> > > static constructor when the application is closed? The unmanaged
code
> > seems
> > > to be leaving this stuff out there. Not allow a static destructor
seems
> > to
> > > allow for memory leaks possibilities. Is there another solution
that
> can
> > do
> > > the same thing as a static destructor that would ONLY be called
> > > AUTOMATICALLY once the application is closed.
> > >
> > > Anyone at Microsoft have a "Good" reason why it was left out? And
do
> they
> > > plan on adding it to a future release?
> > >
> >
> >
>
>





Thong Nguyen <tum(NOSPAM

10/28/2002 5:46:00 PM

0

Hi Rodney,

You could just use a factory to create your classes and use normal C# class
destructors/finalizers.

I fail to think of a case where this wouldn't do what you want...

^Tum

"Rodney S. Foley" <aalst@aalst.com> wrote in message
news:#86CsfTfCHA.2440@tkmsftngp08...
> Why would Microsoft allow a Static Constructor, but not allow a Static
> Destructor?
>
> If you construct something, wouldn't it be nice to be able to destruct it
> regardless of if it was constructed statically?? Especially if you are
> dealing with unmanaged items in a static constructor, it would be nice to
> tell the unmanaged code that you are done with it's stuff. :) So what
> happened to Classes that did some static stuff with unmanaged code in a
> static constructor when the application is closed? The unmanaged code
seems
> to be leaving this stuff out there. Not allow a static destructor seems
to
> allow for memory leaks possibilities. Is there another solution that can
do
> the same thing as a static destructor that would ONLY be called
> AUTOMATICALLY once the application is closed.
>
> Anyone at Microsoft have a "Good" reason why it was left out? And do they
> plan on adding it to a future release?
>
>




Thomas Tomiczek

10/28/2002 5:55:00 PM

0

Wrong :-)


Here is the answer:

Static CONSTRUCTOR: called once when the type is loaded, normally around the
time the first instance is created.
Static DESTRUCTOR: WOULD be called when a type is deleted. BUT - types are
never deleted. Makes no sense.

--
Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)
--
Still writing SQL and dealing with DataSets?
Why dont you use our EntityBroker - the most advanced o/r and business
object toolkit in town.


"Thong Nguyen" <tum(NOSPAM)@veridicus.com> wrote in message
news:#NneyFqfCHA.1692@tkmsftngp09...
> Hi Rodney,
>
> You could just use a factory to create your classes and use normal C#
class
> destructors/finalizers.
>
> I fail to think of a case where this wouldn't do what you want...
>
> ^Tum
>
> "Rodney S. Foley" <aalst@aalst.com> wrote in message
> news:#86CsfTfCHA.2440@tkmsftngp08...
> > Why would Microsoft allow a Static Constructor, but not allow a Static
> > Destructor?
> >
> > If you construct something, wouldn't it be nice to be able to destruct
it
> > regardless of if it was constructed statically?? Especially if you are
> > dealing with unmanaged items in a static constructor, it would be nice
to
> > tell the unmanaged code that you are done with it's stuff. :) So what
> > happened to Classes that did some static stuff with unmanaged code in a
> > static constructor when the application is closed? The unmanaged code
> seems
> > to be leaving this stuff out there. Not allow a static destructor seems
> to
> > allow for memory leaks possibilities. Is there another solution that
can
> do
> > the same thing as a static destructor that would ONLY be called
> > AUTOMATICALLY once the application is closed.
> >
> > Anyone at Microsoft have a "Good" reason why it was left out? And do
they
> > plan on adding it to a future release?
> >
> >
>
>
>
>


Chad Myers

10/28/2002 6:31:00 PM

0

I think his point is that he wants to deinitialize everything
when the app is shutting down.

Theoretically, the type is deleted/unloaded when the app is shutting
down or the app domain is being unloaded, so it could run then.

I think there are other ways of determining when your app domain
is being unloaded so you can do something about it.

The AppDomain.DomainUnload event looks promising.

-c

"Thomas Tomiczek [MVP]" <t.tomiczek@thona-consulting.com> wrote in
message news:uoENkKqfCHA.2552@tkmsftngp12...
> Wrong :-)
>
>
> Here is the answer:
>
> Static CONSTRUCTOR: called once when the type is loaded, normally
around the
> time the first instance is created.
> Static DESTRUCTOR: WOULD be called when a type is deleted. BUT - types
are
> never deleted. Makes no sense.
>
> --
> Regards
>
> Thomas Tomiczek
> THONA Consulting Ltd.
> (Microsoft MVP C#/.NET)
> --
> Still writing SQL and dealing with DataSets?
> Why dont you use our EntityBroker - the most advanced o/r and
business
> object toolkit in town.
>
>
> "Thong Nguyen" <tum(NOSPAM)@veridicus.com> wrote in message
> news:#NneyFqfCHA.1692@tkmsftngp09...
> > Hi Rodney,
> >
> > You could just use a factory to create your classes and use normal
C#
> class
> > destructors/finalizers.
> >
> > I fail to think of a case where this wouldn't do what you want...
> >
> > ^Tum
> >
> > "Rodney S. Foley" <aalst@aalst.com> wrote in message
> > news:#86CsfTfCHA.2440@tkmsftngp08...
> > > Why would Microsoft allow a Static Constructor, but not allow a
Static
> > > Destructor?
> > >
> > > If you construct something, wouldn't it be nice to be able to
destruct
> it
> > > regardless of if it was constructed statically?? Especially if
you are
> > > dealing with unmanaged items in a static constructor, it would be
nice
> to
> > > tell the unmanaged code that you are done with it's stuff. :) So
what
> > > happened to Classes that did some static stuff with unmanaged code
in a
> > > static constructor when the application is closed? The unmanaged
code
> > seems
> > > to be leaving this stuff out there. Not allow a static destructor
seems
> > to
> > > allow for memory leaks possibilities. Is there another solution
that
> can
> > do
> > > the same thing as a static destructor that would ONLY be called
> > > AUTOMATICALLY once the application is closed.
> > >
> > > Anyone at Microsoft have a "Good" reason why it was left out? And
do
> they
> > > plan on adding it to a future release?
> > >
> > >
> >
> >
> >
> >
>
>


Jon Skeet

10/28/2002 6:31:00 PM

0

Thomas Tomiczek [MVP] <t.tomiczek@thona-consulting.com> wrote:
> Here is the answer:
>
> Static CONSTRUCTOR: called once when the type is loaded, normally around the
> time the first instance is created.
> Static DESTRUCTOR: WOULD be called when a type is deleted. BUT - types are
> never deleted. Makes no sense.

Not quite: a type is deleted (or at least becomes eligible for garbage
collection) when the appdomain which loaded the assembly it's part of is
unloaded. At least, that's the way I understand it. Rarely a useful time
to do stuff, I'd have thought...

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.c...
If replying to the group, please do not mail me too

David Banister

10/28/2002 6:42:00 PM

0

A static destructor seems to only make sense in bad code. There shouldn't be
any unmanaged static members of any given type. All constructors are
technically static due to the fact that the constructor isn't run on an
instance of a type. Static destructors don't make sense because you have to
have something to destruct. You cannot run a destructor on a type, you have
to run it on an object.

"Chad Myers" <cmyers@N0.SP.4M.austin.rr.com> wrote in message
news:Rvev9.220511$8o3.6388858@twister.austin.rr.com...
> I think his point is that he wants to deinitialize everything
> when the app is shutting down.
>
> Theoretically, the type is deleted/unloaded when the app is shutting
> down or the app domain is being unloaded, so it could run then.
>
> I think there are other ways of determining when your app domain
> is being unloaded so you can do something about it.
>
> The AppDomain.DomainUnload event looks promising.
>
> -c
>
> "Thomas Tomiczek [MVP]" <t.tomiczek@thona-consulting.com> wrote in
> message news:uoENkKqfCHA.2552@tkmsftngp12...
> > Wrong :-)
> >
> >
> > Here is the answer:
> >
> > Static CONSTRUCTOR: called once when the type is loaded, normally
> around the
> > time the first instance is created.
> > Static DESTRUCTOR: WOULD be called when a type is deleted. BUT - types
> are
> > never deleted. Makes no sense.
> >
> > --
> > Regards
> >
> > Thomas Tomiczek
> > THONA Consulting Ltd.
> > (Microsoft MVP C#/.NET)
> > --
> > Still writing SQL and dealing with DataSets?
> > Why dont you use our EntityBroker - the most advanced o/r and
> business
> > object toolkit in town.
> >
> >
> > "Thong Nguyen" <tum(NOSPAM)@veridicus.com> wrote in message
> > news:#NneyFqfCHA.1692@tkmsftngp09...
> > > Hi Rodney,
> > >
> > > You could just use a factory to create your classes and use normal
> C#
> > class
> > > destructors/finalizers.
> > >
> > > I fail to think of a case where this wouldn't do what you want...
> > >
> > > ^Tum
> > >
> > > "Rodney S. Foley" <aalst@aalst.com> wrote in message
> > > news:#86CsfTfCHA.2440@tkmsftngp08...
> > > > Why would Microsoft allow a Static Constructor, but not allow a
> Static
> > > > Destructor?
> > > >
> > > > If you construct something, wouldn't it be nice to be able to
> destruct
> > it
> > > > regardless of if it was constructed statically?? Especially if
> you are
> > > > dealing with unmanaged items in a static constructor, it would be
> nice
> > to
> > > > tell the unmanaged code that you are done with it's stuff. :) So
> what
> > > > happened to Classes that did some static stuff with unmanaged code
> in a
> > > > static constructor when the application is closed? The unmanaged
> code
> > > seems
> > > > to be leaving this stuff out there. Not allow a static destructor
> seems
> > > to
> > > > allow for memory leaks possibilities. Is there another solution
> that
> > can
> > > do
> > > > the same thing as a static destructor that would ONLY be called
> > > > AUTOMATICALLY once the application is closed.
> > > >
> > > > Anyone at Microsoft have a "Good" reason why it was left out? And
> do
> > they
> > > > plan on adding it to a future release?
> > > >
> > > >
> > >
> > >
> > >
> > >
> >
> >
>
>


Jon Skeet

10/28/2002 7:04:00 PM

0

David Banister <invalidemailaddress@weresoft.net> wrote:
> A static destructor seems to only make sense in bad code. There shouldn't be
> any unmanaged static members of any given type. All constructors are
> technically static due to the fact that the constructor isn't run on an
> instance of a type.

Well, it's static in that you don't have to *provide* an instance in
order to call it, but it's not static in that the code itself of a
constructor can access instance members. It's as if there's a transition
between static and non-static between the client making the call and the
constructor itself being invoked. That transition period is (I would
say) precisely when the memory is allocated for the object.

All of this is irrelevant to what's meant in C# by a static constructor,
though.

> Static destructors don't make sense because you have to
> have something to destruct. You cannot run a destructor on a type, you have
> to run it on an object.

But static constructors (in the C# sense) are effectively run on a type
as part of loading that type (or resolving it, or whatever the C# spec
calls it - I'm afraid I'm on VNC over a VPN, and can't be bothered to
chkec the terminology right now) so it would make sense to have a static
destructor to be called when the type is unloaded. Except it wouldn't
actually be terribly useful in many situations, which I presume was why
it wasn't included. As Chad has mentioned, there's a perfectly good
event which is fired when an AppDomain is unloaded, which is the
earliest time the type can become eligible for garbage collection.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.c...
If replying to the group, please do not mail me too