Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2004

Tip: Looking for answers? Try searching our database.

freeing resources allocated in a static bloc initilaisation of a class ?

Thread view: 
dpr - 24 May 2004 14:07 GMT
I hava a class in wich I initialize a resource like this:

class Dummy {
    static GlobalRessourceClass myGlobalRes;
    {
        // Static bloc initialisation
        myGlobalRes = AllocRessource();
    }
};

How can I do to free the ressource when the program exists ?

Thanks in advance.
Chris Smith - 24 May 2004 14:17 GMT
> I hava a class in wich I initialize a resource like this:
>
[quoted text clipped - 7 lines]
>
> How can I do to free the ressource when the program exists ?

First of all, are you sure you need to?  It's extremely rare to find a
resource on any modern operating system that isn't automatically
reclaimed when a process exits.

If you need to do this, the safest way is generally to create an outside
watchdog process that waits for this one to end and then reclaims the
resources from the outside.  Alternatively, add the logic to the normal
exit case and then add a note in the documentation about manually
cleaning up the resource if it is missed.

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

dpr - 24 May 2004 15:45 GMT
Chris Smith a écrit :

>>I hava a class in wich I initialize a resource like this:
>>
[quoted text clipped - 9 lines]
>
> First of all, are you sure you need to?

Actually the allocation of ressource results, at the end, in sending a
initialization command to a specific hardware. I need to send a
"terminate" command.

 It's extremely rare to find a
> resource on any modern operating system that isn't automatically
> reclaimed when a process exits.
[quoted text clipped - 4 lines]
> exit case and then add a note in the documentation about manually
> cleaning up the resource if it is missed.

Thanks.
Pedro - 29 May 2004 18:32 GMT
> Chris Smith a écrit :
>
[quoted text clipped - 28 lines]
>
> Thanks.

You can add a shutdown hook. See
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#addShutdownHook(j
ava.lang.Thread
)

The Thread object passed as parameter will be executed when the Java virtual machine exits.

Regards,
Pedro


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.