Skip to content

Commit b3edaa0

Browse files
Reef-7sanchitmonga22
authored andcommitted
Fix Guard racFree calls to prevent cleanup failures when rac_free symbol is missing
1 parent 1511aca commit b3edaa0

1 file changed

Lines changed: 17 additions & 17 deletions

File tree

sdk/runanywhere-flutter/packages/runanywhere/lib/native/dart_bridge_voice_agent.dart

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ import 'package:runanywhere/native/ffi_types.dart';
2020
import 'package:runanywhere/native/native_functions.dart';
2121
import 'package:runanywhere/native/platform_loader.dart';
2222

23+
void _safeRacFree(Pointer<Void> ptr) {
24+
if (ptr == nullptr) return;
25+
26+
try {
27+
NativeFunctions.racFree(ptr);
28+
} catch (_) {
29+
// rac_free may not exist in some native builds
30+
}
31+
}
32+
2333
/// VoiceAgent component bridge for C++ interop.
2434
///
2535
/// Orchestrates LLM, STT, TTS, and VAD components for voice conversations.
@@ -76,10 +86,10 @@ class DartBridgeVoiceAgent {
7686
// Use shared component handles (matches Swift approach)
7787
// This allows the voice agent to use already-loaded models from the
7888
// individual component bridges (STT, LLM, TTS, VAD)
79-
final llmHandle = await DartBridgeLLM.shared.getHandle();
80-
final sttHandle = await DartBridgeSTT.shared.getHandle();
81-
final ttsHandle = await DartBridgeTTS.shared.getHandle();
82-
final vadHandle = await DartBridgeVAD.shared.getHandle();
89+
final llmHandle = DartBridgeLLM.shared.getHandle();
90+
final sttHandle = DartBridgeSTT.shared.getHandle();
91+
final ttsHandle = DartBridgeTTS.shared.getHandle();
92+
final vadHandle = DartBridgeVAD.shared.getHandle();
8393

8494
_logger.debug(
8595
'Creating voice agent with shared handles: LLM=$llmHandle, STT=$sttHandle, TTS=$ttsHandle, VAD=$vadHandle');
@@ -409,9 +419,7 @@ class DartBridgeVoiceAgent {
409419
return resultPtr.value != nullptr ? resultPtr.value.toDartString() : '';
410420
} finally {
411421
calloc.free(audioPtr);
412-
if (resultPtr.value != nullptr) {
413-
NativeFunctions.racFree(resultPtr.value.cast<Void>());
414-
}
422+
_safeRacFree(resultPtr.value.cast<Void>());
415423
calloc.free(resultPtr);
416424
}
417425
}
@@ -435,9 +443,7 @@ class DartBridgeVoiceAgent {
435443
return resultPtr.value != nullptr ? resultPtr.value.toDartString() : '';
436444
} finally {
437445
calloc.free(promptPtr);
438-
if (resultPtr.value != nullptr) {
439-
NativeFunctions.racFree(resultPtr.value.cast<Void>());
440-
}
446+
_safeRacFree(resultPtr.value.cast<Void>());
441447
calloc.free(resultPtr);
442448
}
443449
}
@@ -471,13 +477,7 @@ class DartBridgeVoiceAgent {
471477
} finally {
472478
calloc.free(textPtr);
473479
// Free the audio data allocated by C++
474-
if (audioPtr.value != nullptr) {
475-
try {
476-
NativeFunctions.racFree(audioPtr.value);
477-
} catch (_) {
478-
// `rac_free` may not exist in some native builds.
479-
}
480-
}
480+
_safeRacFree(audioPtr.value);
481481
calloc.free(audioPtr);
482482
calloc.free(audioSizePtr);
483483
}

0 commit comments

Comments
 (0)