Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6143913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:34:56+00:00 2026-05-23T18:34:56+00:00

I have a library in c++ and I am writing JNI wrapper functions to

  • 0



I have a library in c++ and I am writing JNI wrapper functions to allow me to call it from JAVA. I have got a lot of different functions working but I am having a problem with one in particular.

The function I am trying to wrap looks like:

void Set_Name(std::string invar)

I have matching calls to GetStringUTFChars and ReleaseStringUTFChars as follows:

JNIEXPORT void JNICALL Java_com_metcarob_mys_javaapi_a_1SM_1D_1Orginisation_Set_1Name
  (JNIEnv *p_jEnv, jobject p_jObj, jlong p_nat, jstring invar) {
SM_D_Orginisation* p_SMD = (SM_D_Orginisation*) p_nat;
const char *pPp_invar2 = p_jEnv->GetStringUTFChars( invar, NULL );
if (NULL==pPp_invar2) return;
std::string *pPp_invar = new std::string(pPp_invar2);
p_SMD->Set_Name(*pPp_invar);
    pPp_invar2 = pPp_invar->c_str(); 
    p_jEnv->ReleaseStringUTFChars(invar, pPp_invar2);
    SAFE_DELETE(pPp_invar); 
  }

I am getting an invalid pointer exception when I run this from Java:

*** glibc detected *** /home/robert/Oracle/Middleware/jdk160_21/bin/java: free(): invalid pointer: 0x0841a744 ***
======= Backtrace: =========
/lib32/libc.so.6(+0x6b511)[0xf7637511]
/lib32/libc.so.6(+0x6ce1b)[0xf7638e1b]
/lib32/libc.so.6(cfree+0x6d)[0xf763bf8d]
/home/robert/Oracle/Middleware/jdk160_21/jre/lib/i386/client/libjvm.so(+0x34fc8c)[0xf703bc8c]
/home/robert/Oracle/Middleware/jdk160_21/jre/lib/i386/client/libjvm.so(+0x25139a)[0xf6f3d39a]
/home/robert/Encrypted/Projects/Scout_Management/smbackend/DEBUG/libjavaapi.so(_ZN7JNIEnv_21ReleaseStringUTFCharsEP8_jstringPKc+0x27)[0xe02475c9]
/home/robert/Encrypted/Projects/Scout_Management/smbackend/DEBUG/libjavaapi.so(Java_com_metcarob_mys_javaapi_a_1SM_1D_1Orginisation_Set_1Name+0xd8)[0xe0245467]
[0xf4c0105d]

If I remove the call to ReleaseStringURFChars it works without a problem. Documentation states that I must call ReleaseStringUTFChars for every call I make to GetStringUTFChars to prevent memory leaks.

I have no idea what I am doing wrong or what I should be checking.

Can anyone assist?
Thanks
Robert

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:34:56+00:00Added an answer on May 23, 2026 at 6:34 pm

    You first acquire the pointer with

    const char *pPp_invar2 = p_jEnv->GetStringUTFChars( invar, NULL );
    

    But then you reassign the pointer with

    pPp_invar2 = pPp_invar->c_str();
    

    You must first first release the memory, then reassign the pointer.

    Also, you don’t need to allocate pPp_invar with new and then delete it, a simple p_SMD->Set_Name(pPp_invar2); should suffice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a library for efficient number processing. I have to support different
I am writing a library for CI and I have a method I call
I'm writing some library code, and users of the library sometimes have different opinions
I have a library that I created with some business logic that includes writing
I have been writing a library for my project (for now I am using
I am writing an iOS library to embed Lua in games and have encountered
I have been using Selenium Library with Robot Framework for writing automated tests for
I am writing a fairly large C++ shared-object library, and have run into a
I have a library written in C++, and a wrapper for this library written
tl;dr: C++ plugin needs to call Java .jar library. How to deploy this to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.