JNI C++ Wrapper

P

Philipp Kraus

Hello,

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++
objects?

Thanks

Phil
 
R

Roedy Green

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++
objects?

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
http://mindprod.com
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

JNI return jobjectArray 7
JNI Invocation API example 4
JNI generic type of jobject 6
Trouble with JNI wrapper functions 1
Lexical Analysis on C++ 1
Wrapper functions 1
JNI and GetFieldID 12
JNI & C & EVENTS 2

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top