Having Trouble understanding CNI string allocation

Bryce McKinlay mckinlay@redhat.com
Mon Sep 27 19:19:00 GMT 2004


Where are you keeping the pointers to the java::lang::Strings? They must 
be kept in something that can be seen by the garbage collector - either 
on the stack or in a java array. As Andrew pointed out, you can't keep 
Java object references in C arrays or any structure alocated by malloc 
or C++ "new" because the garbage collector won't see them.

Bryce


Sebastian Kloska wrote:

>  Thank you very much --
>
>  How's about these random errors when doing all String
>  allocation dynamically (i.e. java::lang::String * is
>  only used for one call and then forgotten). Is this
>  explainable from within CNI or should I try to find
>  the misbehaviour in the JDBC driver ?
>
>  BTW I'm sing gcj-3.3.3 as shipped with Fedora Core 2
>
>  Thanks
>
>  Sebastian
>
>
> Andrew Haley wrote:
>
>> Sebastian Kloska writes:
>>  >  > In an attempt to reduce the calls to JvNewStringUTF I was trying
>>  > to store the colum names I like it access in an array of
>>  > 'java::lang::String *', but after initializatons (as a none static
>>  > class array) and befor using these pointers in another class method
>>  > the strings seem to be gone. So it seems I do not realy understand
>>  > the concept of how long I'm alowed to hold a (valid ?) reference
>>  > to an JAVA object (e.g. String) and that somehow the garbage
>>  > collector is 'stealing' these from me....
>>
>> The gcj garbage collector will recycle memory when it determines that
>> there are no live references to it.  The gc won't look inside
>> structures allocated by malloc() or the C++ runtime library's new().
>>
>> If you want to make an array of 'java::lang::String *' you had better
>> make it a JArray<jstring>, not a C++ array, and you'd better make sure
>> that a pointer to that jarray is live in a Java object somewhere, or
>> the strings will be deallocated.
>>
>> Andrew.
>
>
>



More information about the Java mailing list