- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: C++ constructor not working when called throug...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-18-2009 04:52 PM
тАО08-18-2009 04:52 PM
C++ constructor not working when called through Java (JNI)
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-19-2009 12:26 AM
тАО08-19-2009 12:26 AM
Re: C++ constructor not working when called through Java (JNI)
fprintf(stderr, "%p: X::ctor called\n", this);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-19-2009 08:28 AM
тАО08-19-2009 08:28 AM
Re: C++ constructor not working when called through Java (JNI)
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-19-2009 12:00 PM
тАО08-19-2009 12:00 PM
Re: C++ constructor not working when called through Java (JNI)
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++?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-19-2009 12:05 PM
тАО08-19-2009 12:05 PM
Re: C++ constructor not working when called through Java (JNI)
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-20-2009 12:35 PM
тАО08-20-2009 12:35 PM
Re: C++ constructor not working when called through Java (JNI)
Problem has been resolved.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-20-2009 12:35 PM
тАО08-20-2009 12:35 PM