JNI (Win32), GetComponent, GC

M

Manuel Leclerc

Hi, and sorry for the poor english.

I create a native (win32) thread in a java process (by code injection,
with the CreateRemoteThread-LoadLibrary trick).

Next, I use the folowing APIs :
- JNI_GetCreatedJavaVMs
- AttachCurrentThread
- JAWT_GetAWT

Next, I use a win32 API (EnumWindows) and obtain a Windows Handle (type
HWND) belonging to the Java process.

Finally, I use GetComponent and get a "jobject" corresponding to the
'hwnd'.

It works: I am able to call some java methods.

My question:

What about the GC? Should I call some "free" method when I no longer
need to keep the "jobject" ?
 
G

Gordon Beaton

Should I call some "free" method when I no longer need to keep the
"jobject" ?

Possibly...

In native code that that does not *return* to Java, you must
explicitly notify the garbage collector about unused object
references.

In native code that does return to Java, objects will automatically
become eligible for gc when they return, although if you'e creating
lots of objects locally you may need to notify gc before returning.

To notify the garbage collector, use DeleteLocalRef().

Note that the normal reachability rules also apply, so for references
passed outside the native method (in calls to other Java methods or in
return values etc), the objects won't actually become eligible for
collection until they are no longer reachable from those other places.

You can also use a combination of PushLocalFrame() and PopLocalFrame()
around a section of a native method, to get behaviour similar to that
of invoking a method (Push..) and returning from it (Pop...). The
garbage collector gets notified about any objects created within the
local frame when PopLocalFrame() is called.

/gordon
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top