Results 1 to 8 of 8

Thread: Destructor in Java

  1. #1
    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.

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    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.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    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.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    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.

  5. #5
    Join Date
    May 2008
    Posts
    2,012

    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.

  6. #6
    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.

  7. #7
    Join Date
    Jan 2008
    Posts
    1,521

    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();
    }
    };
    ...
    }

  8. #8
    Join Date
    Apr 2008
    Posts
    2,005

    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.

Similar Threads

  1. Replies: 3
    Last Post: 28-08-2012, 12:05 PM
  2. Problem in LinkedList destructor.
    By KAIRU26 in forum Software Development
    Replies: 5
    Last Post: 15-02-2010, 07:14 PM
  3. What is the difference between a destructor and a finalizer?
    By Taylor D in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 10:47 AM
  4. Basic use of destructor
    By Sacchidananda in forum Software Development
    Replies: 3
    Last Post: 19-11-2009, 10:20 AM
  5. Destructor in PHP5
    By Duck in forum Software Development
    Replies: 3
    Last Post: 18-03-2009, 12:01 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,057,483.72407 seconds with 17 queries