.net - PtrToStringChars, how to free memory? -
i'm using ptrtostringchars method pointer character array held managed string. i'm converting character array jstring , returning calling function. before doing this, how free memory occupied character array? code.
system::string *result=l"checking"; const __wchar_t __pin * retval = ptrtostringchars(result); return env->newstring((jchar *)retval,(jsize) wcslen(retval));
here, how free memory pointed retval?
there no memory needs released, ptrtostringchars() returns interior pointer directly points @ system::string buffer. garbage collector knows how find , update when buffer moved.
you indeed need pin pointer gc cannot move buffer while newstring() function executing. cheap kind of pinning, no handle gets created it. very important newstring() copies string content , not passed pointer. pointer becomes invalid after code execution leaves block contains pin.
you use old managed c++ syntax, has been deprecated past decade , not work anymore in vs2015. proper c++/cli syntax is:
system::string^ result = "checking"; pin_ptr<const wchar_t> retval = ptrtostringchars(result); return env->newstring((jchar*)retval, result->length);
Comments
Post a Comment