JDK-4238486 : finalize() calling native function results in JVM crash
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 1999-05-14
  • Updated: 2004-03-25
  • Resolved: 2002-11-04
Related Reports
Duplicate :  
Description

Name: dbT83986			Date: 05/14/99


When a finalize() method calls a native function,
the JVM can unload the native library before all
the finalize() methods are called resulting in
a crash.

For this to occur the run finalizers on exit must
be true:

class HelloWorld
{
        public HelloWorld()
        {
                System.loadLibrary("hello");
        }

        public native void displayHelloWorld();

        protected void finalize()
        {
                displayHelloWorld();
        }

        public static void main(String[] args)
        {
                System.runFinalizersOnExit(true);
                new HelloWorld().displayHelloWorld();
        }
}

The native function displayHelloWorld can be any
function.

It appears that the finalizer for the object that
contains the native library reference is run before
that finalizer for the HelloWorld object.
(Review ID: 57246) 
======================================================================

Comments
WORK AROUND Name: dbT83986 Date: 05/14/99 If the library is loaded in a static initializer the problem does not occur. Ie; public class HelloWorld { public HelloWorld() { } static { System.loadLibrary("hello"); } [...] } However I could not locate any part of the specification that _requires_ native libraries to be loaded in a static initalizer. The tutorial example however does use one. ======================================================================
11-06-2004