JNI C++ Wrapper

Discussion in 'Java' started by Philipp Kraus, Sep 30, 2011.

  1. 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++


    Philipp Kraus, Sep 30, 2011
    1. Advertisements

  2. Roedy Green

    Roedy Green Guest

    On Fri, 30 Sep 2011 14:41:17 +0200, Philipp Kraus
    <> 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.
    Roedy Green, Oct 10, 2011
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley

    IBM's JNI fails where Sun's JNI works

    Alex Hunsley, Nov 3, 2003, in forum: Java
    Alex Hunsley
    Nov 4, 2003
  2. Pasturel Jean-Louis

    Porting JNI Windows under JNI LINUX + Wine ?

    Pasturel Jean-Louis, Feb 29, 2004, in forum: Java
    Pasturel Jean-Louis
    Mar 3, 2004
  3. Rob
  4. Jim

    JNI wrapper class

    Jim, Sep 11, 2005, in forum: Java
    Stefan Schulz
    Sep 12, 2005
  5. Colby Nyce

    Trouble with JNI wrapper functions

    Colby Nyce, Sep 8, 2011, in forum: Java
    Roedy Green
    Sep 8, 2011

Share This Page