[Ipopt] Jipopt Java interface

Carl Laird carl.d.laird at gmail.com
Tue Apr 22 15:08:43 EDT 2008


Hello all,

I must point out that I do not have the Jipopt code so, at the risk of
saying something completely incorrect, let me add my two bits.

Ipopt uses SmartPointers internally to reference objects passed to the
optimize routine. Therefore, even if you create an object and store it
in a regular pointer, if you give that "raw" pointer to Ipopt AND
Ipopt puts the pointer into a SmartPointer, it will be deleted when
its internal reference count hits zero (without you knowing). Since I
do not have the Jipopt code, I cannot say for sure that this is what
happens, however it is consistent with a crash on delete. (The last
SmartPointer deletes the object when it goes out of scope, and then
the code tries to delete the object again). If someone wants to point
me to the Jipopt code, I will be happy to have a look at it in more
detail. (Sorry I have not been keeping up on this interface
development.)

On a second note, someone can correct me if I am wrong, but I believe
that it is perfectly legal to call delete on a NULL pointer in C++.
The expected behavior is for nothing to happen. Therefore, I think the
lines below
>          problem = NULL;
>          delete problem;
do not do anything except set the pointer to NULL - I do not think
that any object is deleted here at all.
Therefore, if Ipopt is not deleting the problem object internally via
SmartPointers, then the code above should lead to a memory leak. If
Ipopt IS deleting the problem object internally via SmartPointers,
then the code above should not be necessary.

I hope this helps or at least (since I am ignorant of the Jipopt code)
does not make matters worse :)

Cheers,

Carl.

On Sat, Apr 19, 2008 at 7:28 AM, Edson Valle <edsoncv at enq.ufrgs.br> wrote:
>            Dear all
>     The pointer must be pointed to NULL before the deletion (JVM/JNI things).
>
>
>  JNIEXPORT void JNICALL Java_org_coinor_Ipopt_FreeIpoptProblem
>  (JNIEnv *env,
>  jobject obj_this,
>  jlong pipopt){
>     // cast back our class
>       Jipopt *problem = (Jipopt *)pipopt;
>
>       if(problem!=NULL){
>          problem = NULL;
>          delete problem;
>     }
>   }
>               that's it
>
>                                                         Edson Valle
>                                                     edsoncv at enq.ufrgs.br
>
>
>  Citando Rafael de Pelegrini Soares <rafael at vrtech.com.br>:
>
>
>
>  > Dear Edson,
>  >
>  > I don't think this is the source of the problem you've experienced,
>  > because the Jipopt object is store in a raw pointer. No smart pointer is
>  > used there (around line 600 of Jipopt.cpp):
>  >
>  > /* create the IpoptProblem */
>  > Jipopt* problem=new Jipopt(env, obj_this, n, m, nele_jac, nele_hess,
>  > index_style);
>  >
>  >
>  > Once no smart pointer is used to store the object a regular 'delete'
>  > should be used to destroy it.
>  >
>  > Hope this helps.
>  > Regards.
>  >
>  >
>  > On Fri, 2008-04-18 at 14:06 -0300, Edson Cordeiro do Valle wrote:
>  >> Hello all
>  >>         I took a look at the Java Native Interface and I found the
>  >> following function:
>  >>
>  >> JNIEXPORT void JNICALL Java_org_coinor_Ipopt_FreeIpoptProblem
>  >> (JNIEnv *env,
>  >> jobject obj_this,
>  >> jlong pipopt){
>  >>     // cast back our class
>  >>     Jipopt *problem = (Jipopt *)pipopt;
>  >>
>  >>     if(problem!=NULL){
>  >>         delete problem;
>  >>     }
>  >> }
>  >>
>  >> I don't think it is necessary since the smartpointers automatically
>  >> cleans the unused references as stated in the documentation:
>  >>
>  >> " As the SmartPtrs go out of scope, the reference count
>  >>  will be decremented and the objects will automatically
>  >>  be deleted."
>  >>
>  >> The calling to this function from java leads the Java Virtual Machine to
>  >> crash, so I suggest this function removal.
>  >>
>  >> Regards
>  >>
>  >
>  >
>
>
>
>  _______________________________________________
>  Ipopt mailing list
>  Ipopt at list.coin-or.org
>  http://list.coin-or.org/mailman/listinfo/ipopt
>



-- 

Carl D. Laird
Assistant Professor,
Artie McFerrin Department of Chemical Engineering,
Texas A&M University
Ph: (979) 458-4514
Email: carl.laird at tamu.edu


More information about the Ipopt mailing list