
Signature
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Gordon Beaton laid this down on his screen :
First of all, thanks to you and also to Chris for your answers, they
were very important to me. I just started programming with JNI and I
still have to understand all its mechanisms...
> It is important to understand that the JNIEnv pointer is specific to
> the thread that it was originally created for. You should never cache
> the JNIEnv anywhere, and absolutely never pass it to a different
> thread. Your receiving thread should use AttachCurrentThread() to get
> its own JNIEnv.
I thought I could pass them to other threads in the way I showed, but I
was wrong :) With AttachCurrentThread() all works! :)
> Similarly, you should create global references from the "jobj" and its
> class before passing them to the receiving thread. As written, these
> are local references that may become invalid after you return from the
> initial JNI call. Make sure you free any such global references when
> the receiving thread has finished.
Same as before, I used the NewGlobalRef() as suggested in the thread
posted by Chris and now all goes well
> In your send() method, you have a serious memory leak and likely
> corruption of the JVM. First, you allocate a buffer and assign it to
[quoted text clipped - 7 lines]
> reference it creates, or use PushLocalFrame()/PopLocalFrame() at
> appropriate places in the code.
Thanks for these hints. The library for now is only a prototype, my
objective is only to see if I can send and receive data on a RS232 with
full interaction with java and then show results to the boss...we have
an app using JavaComm to interact with the RS232, but we discovered a
memory leak, and cannot pass to RXTX library because it doesn't work
with some gsm modems, so I have to check if it's possible to write our
own driver... :'(
Just a question...you said to use ReleaseByteArrayElements() to release
my rawJavaData array, which is a jbyte *...but I saw that jbyte ==
signed char, so I thought using the free() function was the same (as I
already do with the rawData array)...am I wrong?
> Also, it isn't only unnecessary to cast the return type of malloc(),
> it's bad style.
If I don't do that the compiler shows me an error... "cannot convert
from 'void *' to 'unsigned char *' Conversion from 'void*' to pointer
to non-'void' requires an explicit cast" (I tried eliminating the
LPBYTE cast in the rawData array initialization in the send
function)...
Thanks for all.
Gordon Beaton - 07 Mar 2007 17:07 GMT
> I just started programming with JNI and I still have to understand
> all its mechanisms...
In addition to the JNI spec and FAQ, this online book might help:
http://java.sun.com/docs/books/jni/
> Just a question...you said to use ReleaseByteArrayElements() to
> release my rawJavaData array, which is a jbyte *...but I saw that
> jbyte == signed char, so I thought using the free() function was the
> same (as I already do with the rawData array)...am I wrong?
The difference is that you don't know how GetByteArrayElements()
allocated this resource for you. It might be part of a larger struct,
or a pointer directly into the Java array object. The JVM expects to
manage it itself, and by calling free() you are messing with that.
>> Also, it isn't only unnecessary to cast the return type of malloc(),
>> it's bad style.
>
> If I don't do that the compiler shows me an error...
Sorry, I was thinking in C, not C++.
/gordon

Signature
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Elvandar - 08 Mar 2007 07:48 GMT
Gordon Beaton laid this down on his screen :
>> Just a question...you said to use ReleaseByteArrayElements() to
>> release my rawJavaData array, which is a jbyte *...but I saw that
[quoted text clipped - 5 lines]
> or a pointer directly into the Java array object. The JVM expects to
> manage it itself, and by calling free() you are messing with that.
Ok, now it's clearer :). Thanks for your help!
> Also, it isn't only unnecessary to cast the return type of malloc(),
> it's bad style.
Quibble: not if you're writing in C++ it isn't. At least if I remember my C++
correctly, that language does not consider void* to be assignment compatible
with <pointer to xxx> in the way that C does.
-- chris
Gordon Beaton - 07 Mar 2007 16:57 GMT
>> Also, it isn't only unnecessary to cast the return type of malloc(),
>> it's bad style.
>
> Quibble: not if you're writing in C++ it isn't.
Mea culpa - I didn't look close enough at the code, saw malloc() and
thought "C".
/gordon

Signature
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e