Skip to content

fix: make tflite native ownership exception-safe - #205

Merged
mrousavy merged 1 commit into
margelo:mainfrom
patrickkabwe:fix/tflite-native-ownership
Sep 11, 2026
Merged

fix: make tflite native ownership exception-safe#205
mrousavy merged 1 commit into
margelo:mainfrom
patrickkabwe:fix/tflite-native-ownership

Conversation

@patrickkabwe

Copy link
Copy Markdown
Contributor

Previously, createModel() created raw TfLiteModel, TfLiteInterpreterOptions, and TfLiteInterpreter objects manually. The interpreter was later deleted in HybridTfliteModel::~HybridTfliteModel(), while model/options cleanup happened separately in createModel().

That had two issues:

  • if an error happened between native allocations, cleanup could be skipped
  • delegate-backed interpreters need delegate/native resources to stay alive for the interpreter lifetime, but raw pointer ownership made that lifetime hard to guarantee

This change moves native ownership to RAII wrappers:

  • TfLiteModel and TfLiteInterpreterOptions use unique_ptr with TFLite deleters
  • TfLiteInterpreter uses shared_ptr with a TFLite deleter
  • HybridTfliteModel keeps the shared interpreter owner and uses a non-owning raw pointer only for calls

This makes cleanup deterministic, including when construction or tensor allocation throws.

@mrousavy
mrousavy merged commit 1520ad5 into margelo:main Sep 11, 2026
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants