473,761 Members | 7,710 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 6599
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.ne t> wrote in message
news:%2******** ********@TK2MSF TNGP10.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 UnmanagedResour cesManager
{
public void AllocateResourc es() {...}
public void DeallocateResou rces() {...}
public UnmanagedResour cesManager()
{
AllocateResourc es();
}

~UnmanagedResou rcesManager()
{
DeallocateResou rces();
}
}

class MyMainClass
{
private static UnmanagedResour cesManager s_manager;

static MyMainClass()
{
s_manager = new UnmanagedResour cesManager();
}
}
"Dolphin White" <sx***@163.ne t> wrote in message
news:%2******** ********@TK2MSF TNGP10.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******** ********@tk2msf tngp13.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 UnmanagedResour cesManager
{
public void AllocateResourc es() {...}
public void DeallocateResou rces() {...}
public UnmanagedResour cesManager()
{
AllocateResourc es();
}

~UnmanagedResou rcesManager()
{
DeallocateResou rces();
}
}

class MyMainClass
{
private static UnmanagedResour cesManager s_manager;

static MyMainClass()
{
s_manager = new UnmanagedResour cesManager();
}
}
"Dolphin White" <sx***@163.ne t> wrote in message
news:%2******** ********@TK2MSF TNGP10.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 "ExitProces s"), the runtime is always trying to
finalize the objects. When the process just gets killed (though
"TerminateProce ss"), 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_DET ACH notification. "
"100" <10*@100.com> wrote in message
news:eT******** ******@TK2MSFTN GP10.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******** ********@tk2msf tngp13.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 UnmanagedResour cesManager
{
public void AllocateResourc es() {...}
public void DeallocateResou rces() {...}
public UnmanagedResour cesManager()
{
AllocateResourc es();
}

~UnmanagedResou rcesManager()
{
DeallocateResou rces();
}
}

class MyMainClass
{
private static UnmanagedResour cesManager s_manager;

static MyMainClass()
{
s_manager = new UnmanagedResour cesManager();
}
}
"Dolphin White" <sx***@163.ne t> wrote in message
news:%2******** ********@TK2MSF TNGP10.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******** ********@tk2msf tngp13.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 UnmanagedResour cesManager
{
public void AllocateResourc es() {...}
public void DeallocateResou rces() {...}
public UnmanagedResour cesManager()
{
AllocateResourc es();
}

~UnmanagedResou rcesManager()
{
DeallocateResou rces();
}
}

class MyMainClass
{
private static UnmanagedResour cesManager s_manager;

static MyMainClass()
{
s_manager = new UnmanagedResour cesManager();
}
}
"Dolphin White" <sx***@163.ne t> wrote in message
news:%2******** ********@TK2MSF TNGP10.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 RequestFinalize OnShutdown 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 RequestFinalize OnShutdown, 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******** ******@TK2MSFTN GP12.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 "ExitProces s"), the runtime is always trying to
finalize the objects. When the process just gets killed (though
"TerminateProce ss"), 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_DET ACH notification. "
"100" <10*@100.com> wrote in message
news:eT******** ******@TK2MSFTN GP10.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******** ********@tk2msf tngp13.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 UnmanagedResour cesManager
{
public void AllocateResourc es() {...}
public void DeallocateResou rces() {...}
public UnmanagedResour cesManager()
{
AllocateResourc es();
}

~UnmanagedResou rcesManager()
{
DeallocateResou rces();
}
}

class MyMainClass
{
private static UnmanagedResour cesManager s_manager;

static MyMainClass()
{
s_manager = new UnmanagedResour cesManager();
}
}
"Dolphin White" <sx***@163.ne t> wrote in message
news:%2******** ********@TK2MSF TNGP10.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
1909
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 advance.
3
2229
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 hard coding them into the class itself. The question I'm pondering is where would be the best place to load these string resources? The string messages will be the same for every instance of the class, so I was thinking about loading the...
9
2430
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 then I would consider this a serious error in the language implementation, not to mention a pain in the backside :( Also, is it to much to ask that the method Type.GetTypeFromCLSID be documented
17
1990
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 transition as successfully as possible? Perhaps you've made the transition yourself or just have experience with folks who have made the transition. I'm looking for the common types of mistakes that say a Java/C# or even C++ developer may commonly...
6
14413
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
2084
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 images load and save fine, the problem seems to be that the memory used during the bitmap creation isn't being released properly until the python program ends and python exits. The multipage tiff's are created in a loop (ie, creating 3 tiffs from a
7
8223
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
12310
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 c:\mypath\myModule.exp Class1.obj : error LNK2001: unresolved external symbol __imp___CrtDbgReportW Class2.obj : error LNK2001: unresolved external symbol __imp___CrtDbgReportW Class3.obj : error LNK2019: unresolved external symbol...
36
3240
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
9522
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10111
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9948
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7327
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6603
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5215
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2738
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.