4 messages in net.java.dev.jna.usersRe: AW: [jna-users] converting std::s...
FromSent OnAttachments
Schneider JannMay 5, 2009 2:22 am 
Schneider JannMay 5, 2009 5:24 am 
Timothy WallMay 5, 2009 6:05 am 
Schneider JannMay 5, 2009 6:09 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: AW: [jna-users] converting std::string to const char* and returning itActions...
From:Timothy Wall (twal@dev.java.net)
Date:May 5, 2009 6:05:11 am
List:net.java.dev.jna.users

Unless your std::string is explicitly stored somewhere on the heap, it will be "garbage-collected" from the stack upon function return. Different compilers do this at different times; later versions of GCC are aggressive in disposing of std::string objects across function calls.

In this case, you're allocating new memory and returning it; if you don't dispose of it later, you have a memory leak.

JNA doesn't care how or where your native memory is allocated, only that it remains valid until it has time to copy the contents into Java space as the basis for creating a java.lang.String.

On May 5, 2009, at 8:25 AM, Schneider Jann wrote:

Hi,

I solved my problem on the c++ side this way:

string strFilename = saveInternally(); char* cFilename = new char[strfilename.length()]; strcpy(cFilename, strFilename.c_str()); return cFilename; ...

I don't really understand, why i have to do an extra strcpy(..) in this case of returning the string value and in other cases, when i use the string internally i dont have to.. Can anyone please explain this behaviour to me?

regards Jann