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