Operating System - HP-UX
1748208 Members
2841 Online
108759 Solutions
New Discussion юеВ

Re: C++ constructor not working when called through Java (JNI)

 
Michael Leong
New Member

C++ constructor not working when called through Java (JNI)

Hi all,

I made a simple C++ class that has one method: inside it uses the std::string constructor like this:

std::string foobar("Hello world");

It does some basic C++ string ops on this string and ends. I created a shared library from this class, and then tried to call it from C. That works just fine. However, if I try to call the shared library from Java using the JNI, later printfs in the C++ code are telling me that the newly constructed string has length 0 and is empty. Specifically:

f = fopen("test.txt", "w");
fprintf(f, "len = %d; str = %s\n", (int)foobar.size(), foobar.c_str());
fclose(f);

Examining the fprintf result, len = 0, though the string is correctly printed (len = 0; str = Hello world).

My system specifics: HP-UX B.11.23 ia64, with gcc 4.2.3 built for ia-hp-hpux11.23.

Any advice on how to fix this or thoughts about what may be causing it would be most appreciated. Thanks.
-Michael
6 REPLIES 6
Dennis Handly
Acclaimed Contributor

Re: C++ constructor not working when called through Java (JNI)

You might want to check to see whether this class has been destructed. That might set the length to zero. Put a fprintf in your constructor and destructor for that class:
fprintf(stderr, "%p: X::ctor called\n", this);
Michael Leong
New Member

Re: C++ constructor not working when called through Java (JNI)

It doesn't seem to be the case that the class is being destructed.

I switched the sample to one where a C++ function is being called as a static native method in Java from a single main method (hence, no constructors), and the problem still remains.

Also, I guess I didn't mention before, I'm using Java 6 and have all the basic patches required for that installed.
Dennis Handly
Acclaimed Contributor

Re: C++ constructor not working when called through Java (JNI)

>I switched the sample to one where a C++ function is being called

Inside your C++ function you have a local string? That string should be constructed and then destructed at the end of the block.
If this is file scope, have you linked your shlib with g++?
Michael Leong
New Member

Re: C++ constructor not working when called through Java (JNI)

Here's the static C++ function I'm calling, just to be clear.

FILE* f;
std::string foobar("Hello world");
f = fopen("test.txt", "w");
fprintf(f, "len = %d; str = %s\n", (int)foobar.size(), foobar.c_str());
fclose(f);
printf("C++ function done\n");

I switched over to a trial version of aCC instead of GCC and it gets past this, printing out the length properly. However, it has other issues which the GCC version doesn't seem to have. I'll put that in another thread.

If anyone's got theories on how to make it work nicely on GCC, I'm still interested, though.

For anyone with this kind of problem, switching from gcc to aCC got rid of the size issue. You might incur other problems, though.
Michael Leong
New Member

Re: C++ constructor not working when called through Java (JNI)

In case anyone else stumbles across this issue, I fixed it by switching to aCC and using the -mt and +z flags.

Problem has been resolved.
Michael Leong
New Member

Re: C++ constructor not working when called through Java (JNI)

Closing thread.