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
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