Home > Archive > Java Help > July 2004 > Is it must to check isCopy before ReleaseStringUTFChars?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Is it must to check isCopy before ReleaseStringUTFChars?
|
|
| qazmlp 2004-07-28, 9:09 pm |
| My JNI code has the following
const char *tempstr = pJNIEnv->GetStringUTFChars(str, 0);
// Do some processing
pJNIEnv->ReleaseStringUTFChars(str,tempstr);
Is this code safe? Or, is it must to check for isCopy i.e. the value
for the second argument in GetStringUTFChars and then call
ReleaseStringUTFChars() only if isCopy is set to true?
| |
| Chris Uppal 2004-07-28, 9:09 pm |
| qazmlp wrote:
> My JNI code has the following
> const char *tempstr = pJNIEnv->GetStringUTFChars(str, 0);
> // Do some processing
> pJNIEnv->ReleaseStringUTFChars(str,tempstr);
>
> Is this code safe? Or, is it must to check for isCopy i.e. the value
> for the second argument in GetStringUTFChars and then call
> ReleaseStringUTFChars() only if isCopy is set to true?
Your code is doing it right. Every Get...() must be paired with a
Release...().
The 'isCopy' is only there to indicate whether you have been given a pointer
into the JVM's internal memory, which can be used to determine whether it's
safe to treat the bytes as temporary working storage and make changes in-place.
Personally, I can't easily imagine any situation where I'd want to know the
value of 'isCopy'.
-- chris
| |
| Québec 2004-07-30, 3:58 am |
| > The 'isCopy' is only there to indicate whether you have been given a
pointer
> into the JVM's internal memory, which can be used to determine whether
it's
> safe to treat the bytes as temporary working storage and make changes
in-place.
You mean you can change the value in the address that is pointed?
Jean
| |
| Chris Uppal 2004-07-30, 8:56 pm |
| Québec wrote:
>
> You mean you can change the value in the address that is pointed?
Yes, /IF/ you've been told that the bytes have been copied, then it is safe to
modify what you are given -- if you have to.
As I hinted, though, I don't suppose many programmers ever will have to.
(Since they have to write the code to deal with the case where the bytes are
/not/ a copy anyway, why not use that code in both cases and keep things simple
?)
-- chris
| |
| Québec 2004-07-30, 8:57 pm |
| #define JNI_TRUE 1
const char *fmt = (*env)->GetStringUTFChars(env, jstr, &iscopy);
I can print the string so it is copied.
printf("%d\n", iscopy);
I have 1. Does 1 means true?
I cant modify the string. The compiler tells me it is a constant.
for(i=0;i<11;i++){
for(j=0;j<65;j++){
if(alphabet[j] == fmt[i]){
printf("%d %c\n", i, fmt[i]);
fmt[i] = alphabet[65-j]; <========================
printf("%d %c\n", i, fmt[i]);
break;
}
}
}
Jean
"Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> a écrit dans le
message de news:yL-dncNQJsoFtpfcRVn-hw@nildram.net...
> Québec wrote:
>
> Yes, /IF/ you've been told that the bytes have been copied, then it is
safe to
> modify what you are given -- if you have to.
>
> As I hinted, though, I don't suppose many programmers ever will have to.
> (Since they have to write the code to deal with the case where the bytes
are
> /not/ a copy anyway, why not use that code in both cases and keep things
simple
> ?)
>
> -- chris
>
>
| |
| Chris Uppal 2004-07-31, 8:56 am |
| Québec wrote:
> #define JNI_TRUE 1
> const char *fmt = (*env)->GetStringUTFChars(env, jstr, &iscopy);
> I can print the string so it is copied.
>
> printf("%d\n", iscopy);
>
> I have 1. Does 1 means true?
/anything/ except 0 means true. (See below).
> I cant modify the string. The compiler tells me it is a constant.
>
> for(i=0;i<11;i++){
> for(j=0;j<65;j++){
> if(alphabet[j] == fmt[i]){
> printf("%d %c\n", i, fmt[i]);
> fmt[i] = alphabet[65-j]; <========================
> printf("%d %c\n", i, fmt[i]);
> break;
> }
> }
> }
Boy! This loop is /seriously/ inefficient. Take a look at the OS-supplied, or
language-defined procedures for case mapping.
The reason that the compiler won't let you change the data pointed to by 'fmt'
is that you've told it that you don't want to (with the 'const' declaration).
The following is serious advice, please take it as meant helpfully, not as a
put-down. You seem to be a beginer at C (or a mixture of C and a bit of C++,
which is what you are using here). If you are still at the stage of not
knowing that 0 means false, and everything else means true, then you are
clearly /very much/ a beginner. That simply won't work -- JNI is fairly
complicated, is designed for low-level programming, and is not something that a
non-C programmer can expect to be able to "muddle-though" without knowing what
they are doing. Please take some time to learn C better before going back to
this JNI project. If that's impossible (as I suppose it probably is) then you
/have/ to find a real C programmer who has the time and patience to help you
out. I /very/ much doubt whether there are enough people who contribute to
this newsgroup who have the considerable time it would take to tutor you by
remote control. You stand in danger of constructing something that /seems/ to
work, but is actually full of holes that you just don't know about.
But, good luck anyway !
-- chris
| |
| Québec 2004-07-31, 3:57 pm |
| I had good luck with a lot of work :-)
Thanks
|
|
|
|
|