Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-10-2008
Member
 
Join Date: Apr 2008
Posts: 32
Destructor in Java

I am making a wrapper around an existing C library through JNI. I have two functions Init() and Free() which allocates and deallocates resources. How can I have the wrapper class call Free() when it runs out of scope?

The alternative is ofcourse to let users of the wrapper class call the Free function specifically.
Reply With Quote
  #2  
Old 25-10-2008
Member
 
Join Date: Feb 2008
Posts: 1,848
Re: Destructor in Java

You will have to call Free() explicitely. You can do that from a finalizer, but IMO it's better to have an explicit dispose() method that users of your class should call.
Reply With Quote
  #3  
Old 25-10-2008
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Destructor in Java

Although this is 100% correct, in practise the finalize-mechanism works much better than it should do theoretically.

So, perhaps it is a good idea to go both ways: Explain the "users" that they have to call it and call it in the finalizer as well. (Of course you must ensure that there are no problems when dispose() is called twice.)

Note that there are also mechanisms like shutdownHook() and the java.lang.ref-package which might do the job for certain types of applications as well.
Reply With Quote
  #4  
Old 25-10-2008
Member
 
Join Date: May 2008
Posts: 2,293
Re: Destructor in Java

Agreed, although that isn't /always/ feasible.

However, even if it /is/ feasible, then I'd still recommend using a finalize() method as a fallback cleanup method. Depending on your attitude and application (and religion you might want to make the finalize() method write an error report somewhere if it finds that the resource in question has not been released already.
Reply With Quote
  #5  
Old 25-10-2008
Member
 
Join Date: May 2008
Posts: 2,008
Re: Destructor in Java

I think there's a common idiom for these kind of situations. Have the finalizer call dispose (or have them both call a common 3rd private method). Before doing the actual dispose, check a flag to see if it has already been disposed. If not, do your disposing (in this case call "Free()") and then set the flag.

From there, it's a design choice whether the second (or subsequent) calls to dispose should be ignored, throw an exception or what. But with this pattern, you'll never accidentally do the disposing twice.
Reply With Quote
  #6  
Old 25-10-2008
Member
 
Join Date: Aug 2008
Posts: 69
Re: Destructor in Java

Thanks for your suggestions.

What I did was to make the Free() function available through the wrapper API and then also add a finalize function:

Code:
protected void finalize()
{
Free();
}
My code already takes care of problems regarding calling it multiple times. A handle is set to zero after first cleanup so on 2nd run it won't have any
effect.

What I did notice however is that it the finalize function doesn't explicitly get called on application exit. I added a test printout and first when I called System.runFinalizersOnExit(true) it worked.

Still it's something I will document so it should be ok.
Reply With Quote
  #7  
Old 25-10-2008
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: Destructor in Java

It's best if the finaliser method exactly match what is declared in Object. Changing the exceptions thrown can disrupt subclasses. You should call the super class' finaliser even if it redefine it. In any case, it's better to move the finaliser to a guard object, so that it doesn't become part of your interface and you are not dependent on any subclasses calling their super.

Code:
public class MyResource {
private final Object guard = new Object() {
@Override
protected void finalize() throws Throwable {
Free();
super.finalize();
}
};
...
}
Reply With Quote
  #8  
Old 25-10-2008
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Destructor in Java

Well spotted. Not only is there no guarantee when a finalizer will get called, there is no guarantee that it will ever be called, especially in short-lived applications. This is why you should never rely on the finalizer to switch off the whirling blades or shut down the antimatter injectors.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags:



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Destructor in Java"
Thread Thread Starter Forum Replies Last Post
Dinobot Destructor Pack and Massive Fury Pack DLC for Transformers: Fall of Cybertron cech Video Games 3 28-08-2012 12:05 PM
Problem in LinkedList destructor. KAIRU26 Software Development 5 15-02-2010 07:14 PM
What is the difference between a destructor and a finalizer? Taylor D Software Development 5 27-01-2010 10:47 AM
Basic use of destructor Sacchidananda Software Development 3 19-11-2009 10:20 AM
Destructor in PHP5 Duck Software Development 3 18-03-2009 12:01 AM


All times are GMT +5.5. The time now is 08:16 AM.