Using peer classes and implementation pointers to call C++ classes from java using JNI seems to work out ok so far. Forget the finalize function I talked about in my last post though. Apparently sometimes the finalize function is called and sometimes the finalize function is not called. It’s very unpredictable. This behavior was verified when I did some debugging. My finalize function never got called, so the implementation pointer I created in the JNI was never released.
Instead you need to specify a Java function in your peer class to release the memory in the JNI and explicitly call it yourself. For my java application, I have a JFrame to which I added a windowClosing action listener:
public void windowClosing(WindowEvent e)
// call the destroy method of the Java peer class
System.exit(0); //calling this method is a must
This function is automatically called when the JFrame exits. The destroy function in the peer class calls the JNI destroy function which releases the C++ pointer.