Skip to content

Conversation

@Shlok-Saxena
Copy link
Contributor

This pull request

Fixes Python C-API reference handling issues in the SOFIE Keras parser (tmva/sofie_parsers/src/RModelParser_Keras.cxx) to avoid memory leaks during model parsing.

The Keras parser relies on Python C-API calls (dictionary lookups, attribute access, string conversions). A few utilities and attribute extraction paths were creating temporary Python objects (new references) without releasing them, which could lead to increasing memory usage when parsing multiple models or repeatedly parsing within the same ROOT session.

This PR ensures temporary Python objects created during parsing are properly released, improving stability when parsing multiple models in the same session.

Changes or fixes:

  • Fixed memory leak in GetValueFromDict() by avoiding leaking temporary PyUnicode key objects created via PyUnicode_FromString()
  • Simplified and corrected PyStringAsString() implementation by using PyUnicode_AsUTF8() for safe UTF-8 extraction (avoids leaking temporary objects and unsafe lifetime issues)
  • Fixed Python reference leaks in MakeKerasBatchNorm() by properly releasing PyObject* returned from PyObject_GetAttrString() when retrieving BatchNorm weight names (gamma, beta, moving_mean, moving_variance)

Checklist:

  • tested changes locally
  • updated the docs (if necessary)

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