Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Is it must to check isCopy before ReleaseStringUTFChars?
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?

Report this thread to moderator Post Follow-up to this message
Old Post
qazmlp
07-29-04 02:09 AM


Re: Is it must to check isCopy before ReleaseStringUTFChars?
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-pla
ce.

Personally, I can't easily imagine any situation where I'd want to know the
value of 'isCopy'.

-- chris




Report this thread to moderator Post Follow-up to this message
Old Post
Chris Uppal
07-29-04 02:09 AM


Re: Is it must to check isCopy before ReleaseStringUTFChars?
> 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



Report this thread to moderator Post Follow-up to this message
Old Post
Québec
07-30-04 08:58 AM


Re: Is it must to check isCopy before ReleaseStringUTFChars?
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 sim
ple
?)

-- chris



Report this thread to moderator Post Follow-up to this message
Old Post
Chris Uppal
07-31-04 01:56 AM


Re: Is it must to check isCopy before ReleaseStringUTFChars?
#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
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
Québec
07-31-04 01:57 AM


Re: Is it must to check isCopy before ReleaseStringUTFChars?
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 'fm
t'
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 tha
t a
non-C programmer can expect to be able to "muddle-though" without knowing wh
at
they are doing.  Please take some time to learn C better before going back t
o
this JNI project.  If that's impossible (as I suppose it probably is) then y
ou
/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




Report this thread to moderator Post Follow-up to this message
Old Post
Chris Uppal
07-31-04 01:56 PM


Re: Is it must to check isCopy before ReleaseStringUTFChars?
I had good luck with a lot of work :-)

Thanks



Report this thread to moderator Post Follow-up to this message
Old Post
Québec
07-31-04 08:57 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:31 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.