Skip to content

Commit 406f4e3

Browse files
authored
android : fix-pointer-dangling (#20974)
1 parent 53dc8b5 commit 406f4e3

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

examples/llama.android/lib/src/main/cpp/ai_chat.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,13 @@ Java_com_arm_aichat_internal_InferenceEngineImpl_processSystemPrompt(
365365
const auto *system_prompt = env->GetStringUTFChars(jsystem_prompt, nullptr);
366366
LOGd("%s: System prompt received: \n%s", __func__, system_prompt);
367367
std::string formatted_system_prompt(system_prompt);
368-
env->ReleaseStringUTFChars(jsystem_prompt, system_prompt);
369368

370369
// Format system prompt if applicable
371370
const bool has_chat_template = common_chat_templates_was_explicit(g_chat_templates.get());
372371
if (has_chat_template) {
373372
formatted_system_prompt = chat_add_and_format(ROLE_SYSTEM, system_prompt);
374373
}
374+
env->ReleaseStringUTFChars(jsystem_prompt, system_prompt);
375375

376376
// Tokenize system prompt
377377
const auto system_tokens = common_tokenize(g_context, formatted_system_prompt,
@@ -414,13 +414,13 @@ Java_com_arm_aichat_internal_InferenceEngineImpl_processUserPrompt(
414414
const auto *const user_prompt = env->GetStringUTFChars(juser_prompt, nullptr);
415415
LOGd("%s: User prompt received: \n%s", __func__, user_prompt);
416416
std::string formatted_user_prompt(user_prompt);
417-
env->ReleaseStringUTFChars(juser_prompt, user_prompt);
418417

419418
// Format user prompt if applicable
420419
const bool has_chat_template = common_chat_templates_was_explicit(g_chat_templates.get());
421420
if (has_chat_template) {
422421
formatted_user_prompt = chat_add_and_format(ROLE_USER, user_prompt);
423422
}
423+
env->ReleaseStringUTFChars(juser_prompt, user_prompt);
424424

425425
// Decode formatted user prompts
426426
auto user_tokens = common_tokenize(g_context, formatted_user_prompt, has_chat_template, has_chat_template);

0 commit comments

Comments
 (0)