Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!news.cgarbs.de!news.addix.net!feed.news.schlund.de!schlund.de!news.online.de!not-for-mail From: Philipp Kraus Newsgroups: comp.lang.java.programmer Subject: JNI C++ Wrapper Date: Fri, 30 Sep 2011 14:41:17 +0200 Organization: 1&1 Internet AG Lines: 71 Message-ID: NNTP-Posting-Host: p3ee289b0.dip0.t-ipconnect.de Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Trace: online.de 1317386477 17695 62.226.137.176 (30 Sep 2011 12:41:17 GMT) X-Complaints-To: abuse@einsundeins.com NNTP-Posting-Date: Fri, 30 Sep 2011 12:41:17 +0000 (UTC) User-Agent: Unison/2.1.5 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8410 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