...
The ordering problem can be dangerous when dealing with native code. For example, if object A
references object B
(either directly or reflectively) and the latter gets finalized first, A
's finalizer may end up dereferencing dangling native pointers. To impose an explicit ordering on finalizers, make sure that B
is remains reachable before until A
's finalizer has concluded. This can be achieved by adding a reference to B
in some global state variable and removing it as soon as when A
's finalizer gets executedexecutes. An alternative is to use the java.lang.ref
references.
...