Raif S. Naffah wrote;
> i followed the advice given in [1] and synchronized the code 
> in the BigInteger finalize() method which now looks like so:

This is not yet sufficient. *All* native method calls that use the
native_ptr need to be synchronized.

It is also not very good practice to synchronize on the object itself
and you really shouldn't have a finalize method that is non-final
(otherwise untrusted code can leak unmanaged memory by allocating
BigInteger subclasses that override the finalize and don't call the base
class method).

When working with a pointer to native memory (or a handle) the only
reliable pattern is really:

class BigInteger
{
  private NativeData nd = new NativeData();

  private static final class NativeData
  {
     private Pointer native_data;

     synchronized int Compare(BigInteger x, BigInteger y)
     {
       return natCompare(x.nd.native_data, y.nd.native_data);
     }

     protected synchronized void finalize()
     {
       natFinalize(native_data);
     }

     private native void natFinalize(Pointer p);
     private native int natCompare(Pointer x, Pointer y);
  }
}

Regards,
Jeroen

Reply via email to