Is it must to check isCopy before ReleaseStringUTFChars?

Q

qazmlp

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?
 
C

Chris Uppal

qazmlp said:
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
 
Q

Québec

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
 
C

Chris Uppal

Québec said:
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
 
Q

Québec

#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){
printf("%d %c\n", i, fmt);
fmt = alphabet[65-j]; <========================
printf("%d %c\n", i, fmt);
break;
}
}
}

Jean
 
C

Chris Uppal

Québec said:
#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){
printf("%d %c\n", i, fmt);
fmt = alphabet[65-j]; <========================
printf("%d %c\n", i, fmt);
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
 

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top