This is the mail archive of the
java@gcc.gnu.org
mailing list for the Java project.
Having Trouble understanding CNI string allocation
- From: Sebastian Kloska <kloska at scienion dot de>
- To: java at gcc dot gnu dot org
- Date: Mon, 27 Sep 2004 13:42:54 +0200
- Subject: Having Trouble understanding CNI string allocation
- Organization: Scienion AG
- Reply-to: kloska at scienion dot de
Hi there ....
I'm currently working on a minimal Jdbc interface for C++.
Just want to map some of the Connection,PreparedStatement,
ResultSet etc .. objects into C++ space e.g. by providing
a C++ ResultSet object accepting 'const std::string &' as
a parameter for getString and returning 'std::string' as
an result.
ResultSet is more ore less a container for a 'java::lang::ResultSet'
pointer and methods like 'getString(const std::string &n)' simply
do something like
<snip>
std::string ResultSet::getString(const std::string & n) {
return strAdapter(__jdbcResultSet->getString(strAdapter(n).javaString()));
}
</snip>
Where strAdapter is a 'std::string' subclass wich returns a
java::lang::String * which is dynamically allocated (JvNewStringUTF)
when calling javaString and which can be initialized with
a 'java::lang::String *' by calling JvGetStringUTFRegion.
So when I iterate through a large ResultSet I may end up with
quite a lot of calls to JvNewStringUTF (once to get a new
column name and twice for returning the result of getString
as a std::string derivate.
After about 2000 calls to getString I end up with quite unpredictable
Exception (somethime NullPointerExceptions from the JVM, somtimes
std::exceptin, somethimes abortions without any exception).
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....
Hope that was understandable
Cheers
Sebastian Kloska