C++ constructor not working when called through Java (JNI) (160 Views)
Reply
Occasional Visitor
Michael Leong
Posts: 5
Registered: ‎08-18-2009
Message 1 of 7 (160 Views)

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
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,853
Registered: ‎03-06-2006
Message 2 of 7 (160 Views)

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);
Please use plain text.
Occasional Visitor
Michael Leong
Posts: 5
Registered: ‎08-18-2009
Message 3 of 7 (160 Views)

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.
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,853
Registered: ‎03-06-2006
Message 4 of 7 (160 Views)

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++?
Please use plain text.
Occasional Visitor
Michael Leong
Posts: 5
Registered: ‎08-18-2009
Message 5 of 7 (160 Views)

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.
Please use plain text.
Occasional Visitor
Michael Leong
Posts: 5
Registered: ‎08-18-2009
Message 6 of 7 (160 Views)

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.
Please use plain text.
Occasional Visitor
Michael Leong
Posts: 5
Registered: ‎08-18-2009
Message 7 of 7 (160 Views)

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

Closing thread.
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation