This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

JNI memory allocation


Hi all,

I'm porting a java wrapper for the (http://www.libsdl.org) SDL library to JNI, and was hoping to use things like JvNewByteArray for the memory allocated within the native C++ calls. It struck me, though, that the existing code passes handles (as jint's) back from the C++ code to the java world, and vice versa when calling native code. If this is the case, won't the garbage collector consider the memory (which is only referenced via the handle, which Java thinks is an int, not a pointer) fair game for collection at any time ?

As an example, there are constructs like:

jint sdl::event::EventDispatcher::SDLPollEvent(jint handle)
{
SDL_Event *event = NULL;
if (handle==0)
{
// old code commented out and replaced by JvNewByteArray
// event = (SDL_Event *)malloc(sizeof(SDL_Event));
jbyteArray array = JvNewByteArray(sizeof(SDL_Event));
event = (SDL_Event *) elements(array);
}
else
event = (SDL_Event *)handle;


if (event != NULL)
{
event->type = SDL_NOEVENT;
SDL_PollEvent(event);
return (jint)event;
}
return 0;
}


Once the handle is returned, it's used to refer to the event later on for processing as different types etc.

So, is it safe to use JvNewByteArray in this sort of context, or is it dangerous ?

Thanks in advance for any help - much appreciated :-))

ATB,
   Simon



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]