Why not take advantage of the facilities to guarantee removal, performed automatically ...?
class ConvertStringHelper { public: ConvertStringHelper( JNIEnv *env, jstring value ) { m_str = env->GetStringUTFChars(value, 0); m_value = &value; m_env = env; } ~ConvertStringHelper() { m_env->ReleaseStringUTFChars( *m_value, m_str); } jstring* m_value; const char *m_str; JNIEnv *m_env; };
Then:
ConvertStringHelper helper( env, cmd ); const char* nativeStr = helper.m_str;
source share