Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JNI C++ Wrapper

Thread Tools

JNI C++ Wrapper

Philipp Kraus
Posts: n/a

I would like to create a Java Wrapper around my C++ Klasses / Objects,
so each Java Class should be have a corresponding C++ Class (object).
My first example shows like:

public class jnitest {

static { System.loadLibrary("jnitest"); }

public jnitest( int x ) { cpp_ctor(x); };
public native void setNumber( int n );
public native int getNumber();

private native void cpp_ctor( int x );
private long cpp_ptr;

I create the C++ Code and create within the cpp_ctor call an object on
the heap with jnitest* = new jnitest(value) and set the pointer variable
with SetLongField to the cpp_ptr property within the Java Object.

I can run the native methods on the Java and C++ Object and handle with
the data, but a last I see the problem to call the destructor on the C++
object, because the finalize call on Java is not guaranteed. In a lot
of tutorials I found that the OS should handle this problem, but I think
the structure of the class can create memory leaks, because if only one
method in the class can modify the cpp_ptr content it point to another
point in the heap and I can work with the C++ object.

I've found out that I can overload the new and delete operator in the
JNI interface, so I do something like:

jnitest* cpp_ptr= NULL

void delete( void* p_ptr)
delete(cpp_ptr) or free(cpp_ptr)

but I get in both styles a "bus error" during execution. My next idea
should be a map of the C++ objects, in which I added a new element
with the unique java object hash and a pointer to the C++ object, but
If the map calls its dtor all I can call each destructor of the object.
I have thinked also about the GlobalReference of the JNIEnv object, so
on the JNI c_tor call the C++ object is created and added to a
GlobalReferenc of the JNIEnv, but how I can call the dtor than !?

The next point in my thinking is in C++ named the copy-ctor: So If I
create a new Java object with x = a; it point to the same underlaying
C++ object, in C++ style the coy-ctor creates deep or flat copy of the
object properties. Because the problem, that I see, is the dtor call
If I do something like
public function testfunc (myjavaobject x) {
myjavaobject a = x;

than is a marked for the GC at the end of the function, but the
underlaying object can be exists, so the GC can't remove the object and
the C++ object should be also exists.

Is there a solution to avoid any memory leaks with the dtor call in the
JNI calls or any "default structure" to create a wrapper around C++



Reply With Quote
Roedy Green
Posts: n/a
On Fri, 30 Sep 2011 14:41:17 +0200, Philipp Kraus
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>Is there a solution to avoid any memory leaks with the dtor call in the
>JNI calls or any "default structure" to create a wrapper around C++

Inside your C++ method you must destroy objects you create, or you
must later do a Java call to some C++ code that kills them. You can
use addShutDownHook in Java to arrange for code to run at shutdown.
Roedy Green Canadian Mind Products
It should not be considered an error when the user starts something
already started or stops something already stopped. This applies
to browsers, services, editors... It is inexcusable to
punish the user by requiring some elaborate sequence to atone,
e.g. open the task editor, find and kill some processes.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble with JNI wrapper functions Colby Nyce Java 1 09-08-2011 07:35 PM
WebSphere JVM Access Violation Problem running Java program that calls methods in a Windows C++ DLL via a JNI wrapper....... adgarcia13 Java 2 06-28-2006 11:19 AM
JNI wrapper class Jim Java 2 09-12-2005 09:37 AM
including package name in jni wrapper Rob Java 2 06-14-2004 03:55 PM
IBM's JNI fails where Sun's JNI works Alex Hunsley Java 4 11-04-2003 10:34 AM