473,509 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I release the resources allocated in Static constructors?

For example, I allocate some unmanaged resources in the static constructors,
then how can I properly release the resource before the application exit?

thx!
Nov 15 '05 #1
6 6587
100
Hi Dolphin,
There is no static finalizer ot something like that. Why do you wary about
resources when the process termintaes? Usualy all resources are freed
automatically in that case. If you want to free them explicitly, you can
write designated static method, which can take care of this, but you have to
call this method by your self before shut the application down.

HTH
B\rgds
100

"Dolphin White" <sx***@163.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
For example, I allocate some unmanaged resources in the static constructors, then how can I properly release the resource before the application exit?

thx!

Nov 15 '05 #2
It would be pretty easy to do if you create a special class that knows how
to take care of the unmanaged resources. That way, instead of working with
the resources directly, you'll need to create a (static) instance of that
class in your static constructor. That instance will deallocate the
resources in its finalizer. To illustrate the idea:

class UnmanagedResourcesManager
{
public void AllocateResources() {...}
public void DeallocateResources() {...}
public UnmanagedResourcesManager()
{
AllocateResources();
}

~UnmanagedResourcesManager()
{
DeallocateResources();
}
}

class MyMainClass
{
private static UnmanagedResourcesManager s_manager;

static MyMainClass()
{
s_manager = new UnmanagedResourcesManager();
}
}
"Dolphin White" <sx***@163.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
For example, I allocate some unmanaged resources in the static constructors, then how can I properly release the resource before the application exit?

thx!

Nov 15 '05 #3
100
Hi Val,
Anyway a finalizer are not guaranteed to be executed when the process shuts
down.
AFAIK CLR gives some amount of time for all finalizers to finish when the
process ends and when this time interval expires CLR just shuts the process
down.

B\rgds
100
"Val Savvateev" <1@2.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
It would be pretty easy to do if you create a special class that knows how
to take care of the unmanaged resources. That way, instead of working with
the resources directly, you'll need to create a (static) instance of that
class in your static constructor. That instance will deallocate the
resources in its finalizer. To illustrate the idea:

class UnmanagedResourcesManager
{
public void AllocateResources() {...}
public void DeallocateResources() {...}
public UnmanagedResourcesManager()
{
AllocateResources();
}

~UnmanagedResourcesManager()
{
DeallocateResources();
}
}

class MyMainClass
{
private static UnmanagedResourcesManager s_manager;

static MyMainClass()
{
s_manager = new UnmanagedResourcesManager();
}
}
"Dolphin White" <sx***@163.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
For example, I allocate some unmanaged resources in the static

constructors,
then how can I properly release the resource before the application exit?
thx!


Nov 15 '05 #4
Below is a quote from MSDN. My understanding is that it really depends on
how the process is terminated. When it is terminated in a regular way
(trough "closing" or "ExitProcess"), the runtime is always trying to
finalize the objects. When the process just gets killed (though
"TerminateProcess"), well... there's no program that would be able to do any
clean-up in that case. :)

"The Finalize method might not run to completion or might not run at all in
the following exceptional circumstances:

a.. Another finalizer blocks indefinitely (goes into an infinite loop,
tries to obtain a lock it can never obtain and so on). Because the runtime
attempts to run finalizers to completion, other finalizers might not be
called if a finalizer blocks indefinitely.
b.. The process terminates without giving the runtime a chance to clean
up. In this case, the runtime's first notification of process termination is
a DLL_PROCESS_DETACH notification. "
"100" <10*@100.com> wrote in message
news:eT**************@TK2MSFTNGP10.phx.gbl...
Hi Val,
Anyway a finalizer are not guaranteed to be executed when the process shuts down.
AFAIK CLR gives some amount of time for all finalizers to finish when the
process ends and when this time interval expires CLR just shuts the process down.

B\rgds
100
"Val Savvateev" <1@2.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
It would be pretty easy to do if you create a special class that knows how
to take care of the unmanaged resources. That way, instead of working with the resources directly, you'll need to create a (static) instance of that class in your static constructor. That instance will deallocate the
resources in its finalizer. To illustrate the idea:

class UnmanagedResourcesManager
{
public void AllocateResources() {...}
public void DeallocateResources() {...}
public UnmanagedResourcesManager()
{
AllocateResources();
}

~UnmanagedResourcesManager()
{
DeallocateResources();
}
}

class MyMainClass
{
private static UnmanagedResourcesManager s_manager;

static MyMainClass()
{
s_manager = new UnmanagedResourcesManager();
}
}
"Dolphin White" <sx***@163.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
For example, I allocate some unmanaged resources in the static

constructors,
then how can I properly release the resource before the application

exit?
thx!



Nov 15 '05 #5
Oh, my god! thx!
I have used this way in my VC programs so often, buy I am immersion in the
static constructor of C# and forgot it. :(

Thank you for your comment!
"Val Savvateev" <1@2.com> дÈëÓʼþ
news:%2****************@tk2msftngp13.phx.gbl...
It would be pretty easy to do if you create a special class that knows how
to take care of the unmanaged resources. That way, instead of working with
the resources directly, you'll need to create a (static) instance of that
class in your static constructor. That instance will deallocate the
resources in its finalizer. To illustrate the idea:

class UnmanagedResourcesManager
{
public void AllocateResources() {...}
public void DeallocateResources() {...}
public UnmanagedResourcesManager()
{
AllocateResources();
}

~UnmanagedResourcesManager()
{
DeallocateResources();
}
}

class MyMainClass
{
private static UnmanagedResourcesManager s_manager;

static MyMainClass()
{
s_manager = new UnmanagedResourcesManager();
}
}
"Dolphin White" <sx***@163.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
For example, I allocate some unmanaged resources in the static

constructors,
then how can I properly release the resource before the application exit?
thx!


Nov 15 '05 #6
100
Hi Val,
Even though you might close the application in the *normal way* finalizers
are not quarantee to be executed.
Following is a quote form the Jeffrey Richter'a article "Garbage Collection:
Automatic Memory Management in the Microsoft .NET Framework", which can be
found at
http://msdn.microsoft.com/msdnmag/issues/1100/gci/ - Part I and
http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/ - Part II

"When an application terminates, some objects are still reachable and will
not have their Finalize method called. This can happen if background threads
are using the objects or if objects are created during application shutdown
or AppDomain unloading. In addition, by default, Finalize methods are not
called for unreachable objects when an application exits so that the
application may terminate quickly. Of course, all operating system resources
will be reclaimed, but any objects in the managed heap are not able to clean
up gracefully. You can change this default behavior by calling the System.GC
type's RequestFinalizeOnShutdown method. However, you should use this method
with care since calling it means that your type is controlling a policy for
the entire application."

I couldn't find the method RequestFinalizeOnShutdown, though.

What is not mentioned in the article, but can be found in the Jeffrey
Richter's book "Applied Microsoft.Net Framework Programming in... " is that
for every finalizer CLR gives 2 sec to terminate if a finalizer fails to
terminate in 2 sec the application is shuted down and no other finalizers
are called. For all finalizers CLR gives 40 sec to terminate and when this
time expires the application is shuted down and the rest of the finalizers
remain uncalled.
This time intervals are subject to change and this values are correct for
the time JR was writing the book.

Any ways the time intervals look big enough not to warry about them, but
there are still chances to have the finalizers not called. Anyway, since the
application is terminated and all umanaged resources will be reclaimed I
think the best is not to warry about them.
If the unmanaged resource is not one of the *windows unmanaged resources* (I
mean is some custom device which has to be shuted down explicitly) I think
you should be very carefull before using finalizers for that.

To finalize ;) my post I just want to remind you that MS says the
programmers should avoid using finalizers *only* for cleaning the unmanaged
resources. IDisposable pattern should be used instead.

HTH
B\rgds
100

"Val Savvateev" <1@2.com> wrote in message
news:Oo**************@TK2MSFTNGP12.phx.gbl...
Below is a quote from MSDN. My understanding is that it really depends on
how the process is terminated. When it is terminated in a regular way
(trough "closing" or "ExitProcess"), the runtime is always trying to
finalize the objects. When the process just gets killed (though
"TerminateProcess"), well... there's no program that would be able to do any clean-up in that case. :)

"The Finalize method might not run to completion or might not run at all in the following exceptional circumstances:

a.. Another finalizer blocks indefinitely (goes into an infinite loop,
tries to obtain a lock it can never obtain and so on). Because the runtime
attempts to run finalizers to completion, other finalizers might not be
called if a finalizer blocks indefinitely.
b.. The process terminates without giving the runtime a chance to clean
up. In this case, the runtime's first notification of process termination is a DLL_PROCESS_DETACH notification. "
"100" <10*@100.com> wrote in message
news:eT**************@TK2MSFTNGP10.phx.gbl...
Hi Val,
Anyway a finalizer are not guaranteed to be executed when the process

shuts
down.
AFAIK CLR gives some amount of time for all finalizers to finish when the
process ends and when this time interval expires CLR just shuts the

process
down.

B\rgds
100
"Val Savvateev" <1@2.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
It would be pretty easy to do if you create a special class that knows

how to take care of the unmanaged resources. That way, instead of working with the resources directly, you'll need to create a (static) instance of that class in your static constructor. That instance will deallocate the
resources in its finalizer. To illustrate the idea:

class UnmanagedResourcesManager
{
public void AllocateResources() {...}
public void DeallocateResources() {...}
public UnmanagedResourcesManager()
{
AllocateResources();
}

~UnmanagedResourcesManager()
{
DeallocateResources();
}
}

class MyMainClass
{
private static UnmanagedResourcesManager s_manager;

static MyMainClass()
{
s_manager = new UnmanagedResourcesManager();
}
}
"Dolphin White" <sx***@163.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
> For example, I allocate some unmanaged resources in the static
constructors,
> then how can I properly release the resource before the application

exit?
>
> thx!
>
>



Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1892
by: Rex_chaos | last post by:
I have allocated a array like double a; We know that a 1-D array just like an 1-D pointer. So I wonder that should I release the memory of the array myself like free(a); Thanks in...
3
2215
by: Wavemaker | last post by:
I'm writing a class whose methods can throw exceptions under certain circumstances. These exceptions will need string messages. I was thinking about placing these messages in a resource instead of...
9
2415
by: A J Le Couteur Bisson | last post by:
Could someone please confirm that static class constructors are only called at the first use of a non-static constructor on the class, or am I doing something wrong? If this is indeed the case...
17
1965
by: ToddLMorgan | last post by:
I'm just starting out with python, after having a long history with Java. I was wondering if there were any resources or tips from anyone out there in Python-land that can help me make the...
6
14365
by: laikon | last post by:
Hi, everyone, below is my program to test static pointer data member; class A { private: static A* p; protected: A() {} public: static A* init()
1
2066
by: geskerrett | last post by:
I was wondering if a ctypes expert could point me in the right direction. I am using the wrapper for the "freeimage" library to create multipage TIFF files from a group of png images. The...
7
8204
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
2
12295
by: 2b|!2b==? | last post by:
I am having linkage errors in my release build as ff: ------ Build started: Project: myModule, Configuration: Release Win32 ------ Linking... Creating library c:\mypath\myModule.lib and object...
36
3195
by: mynews | last post by:
I use free() function to release memory, But the memory usage is still increasing. cygwin #include <stdio.h> #include <stdlib.h> #include <string.h> struct queue{
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7505
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...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.