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!

Why cant I delete C++ pointer in JNI DLL

843829Apr 12 2007 — edited Jun 1 2007
Hi,

I'm new to JNI and pretty rusty on C++, normally I stick to 100% Java, however I had a need for some native magic. My code works well and does the job I needed however my (old) knowledge of C++ tells me that I need to clean my objects up when I'm done. However I am unable to delete a pointer and I'm pretty sure its got something to do with JNI and lots to do with my lack of indepth knowledge. Here's what my code does.

I have a Java app (J) that statically loads my DLL

"J" calls a native c++ function within the DLL called (S).

"S" creates a C++ object using the "new" operator, called (T).

"T" is a Thread which I then start ( it runs for 1minute)

"S" does not wait for "T" to finish, instead it returns immediately, passing the pointer to "T" back to "J"

"J" then starts a normal Java Thread and every 10secs calls a native function from the DLL, called (C), and passes the pointer to "T".

"C" then checks "T" to find out if it has finished, if not then it returns % complete as an "int". If "T" has finished it throws an Exception, and then attempts to delete "T" calling the following code :-
delete T;
T = 0;
printf("\ndone\n");
However it crashed on "delete T", if I comment both these lines out everything completes ok.

My first thought was that "T" is already deleted, however I've checked and this is not the case, it still points to a valid "T" object. I know this because if I let my Java Thread continue to query for status long after "T" has stopped it still gets the last status message.

So my problem is, how do I delete "T"?. The system crashes if I try......interestingly enough though if I comment out the "delete T" line and leave the "T=0" line then I get an "Access Violation" error instead.

Does this mean that C++ does not own the object anymore (becase I passed it to Java). Does Java now own it and will it garbage collect it for me?

Thanks for any help
Chris.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 29 2007
Added on Apr 12 2007
88 comments
1,664 views