Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Destroy C++ Objects

843829Aug 23 2004 — edited Aug 23 2004
Good day to all.

In my JNI experiment I have created an object on the C++ side. Meaning, my C++ class has a constructor. I have instantiated the object in my wrapper class and my Java object is "communicating" with it. (able to call methods on it).

My question is whether / how to free memory. I know that in Java if an object is not reachable the GC collects it. I know that JVM has nothing to do with C++ objects. I was told that I could add the

delete(myC++Object) in my wrapper

and it clears the object from memory.

This said, I ran a test. I create the object via JNI and call methods on it.
That works.
I call the delete method and try to call a method, it crashes as expected.
I change my test so that I create 2 objects and invoke methods on one and test the state of both to make sure that I am dealing with only one at a time.
Successful.

I then wrote some unit tests and time trial tests where I create and destroy many objects. I found that if I created and destroyed my object many times, then created it and tried to call methods on it the system crashes. If I remove all of my delete calls, the system doesn't crash.

My questions.
1) Do I have to delete objects that I create through JNI?
2) Why after many instantiations and deletions would the app crash when a method is called on the object?
(I checked the code very well to make sure that I wasn't calling on a deleted object.)

Thank you.


Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 20 2004
Added on Aug 23 2004
2 comments
374 views