Question

I have a super class with telescoping constructors with a finalize() method. To protect against subclasses forgetting to invoke super.finalize, I have written a finalizer guardian (EJ Item 7 ) like so.

public class Super {

    public Super() {}

    {
        Object finalizerGuardian = new Object() {
            @Override
            protected void finalize() {
                System.out.println("Finalizer guardian finalize");
                Super.this.finalize();
            }
        };
    }

    protected void finalize() {
        System.out.println("Super finalize");
    }

}

Here is a sample subclass --

public class Sub extends Super {

    public Sub() {}

    protected void finalize() {
        System.out.println("Sub finalize");
    }

    public static void main(String[] args)
            throws InterruptedException {
        if (1 == 1) {
            Sub s1 = new Sub();
        }
        Runtime.getRuntime().gc();
        Thread.sleep(10000);
    }
}

When the s1 object goes out of scope, the finalizer guardian's finalize() gets called, and I get the SYSO from the subclass's finalize method, but never get the one from super's finalize.

I'm confused. Am I misunderstanding something fundamentally?

Disclaimer : I realize finalizers are dangerous and not advisable, etc. Still trying to understand the problem here.

Was it helpful?

Solution

Effective Java's finalizer guardian should perform the necessary finalization logic itself (for example, call some method of Super that performs the actual finalization) rather than call the finalize() method, because in your case Super.this.finalize(); actually calls the overriden method from the subclass.

Also note that finalizer guardian should be a field of the class:

public class Super {
    private final Object finalizerGuardian = new Object() {
            @Override
            protected void finalize() {
                Super.this.doFinalize();
            }
    };

    private void doFinalize() {
        System.out.println("Super finalize");
    }
}

OTHER TIPS

You are overloading the Super.finalize() method in Sub. That's why it's not being called.

So when you in "finalizerGuardian" call Super.this.finalize(); you are actually calling Sub.finalize().

AS others have already said dynamic dispatch is your problem here. BUT there's one other major bug in your code as far as I see: Your finalizerGuardian is only defined inside the initialization block, this means that as soon as the object is initialized, the object gets out of scope and can be GCed.

Ie even if you fix your first problem (by defining a final method in your class that handles the finalization stuff), you'll still need to save the finalizerGuardian in a instance variable.

Super.finalize() is not getting called because Sub overrides it. Try adding a Super._finalize() method and calling it from Super.finalize() and the finalizer guardian.

Also, I think finalizerGuardian needs to be a field of Super. Otherwise, it can get garbage collected even if the Super object is still strongly reachable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top