Skip to content

[Bug] C++ memory leak when getting a non-empty vector of overridable initializer names #29603

Description

@kareertl

Describe the issue

In include/onnxruntime/onnxruntime_cxx_inline.h, in GetOverridableInitializerNames()'s for-loop, the memory allocated for names isn't freed after use:

  for (size_t i = 0; i < num_initializers; ++i) {
    char* name;
    ThrowOnError(GetApi().SessionGetOverridableInitializerName(this->p_, i, allocator, &name)); // memory allocated
    initializer_names.emplace_back(name); // name copied
    // missing allocator.Free(name);
  }

  return initializer_names;

To reproduce

Track memory and run session.GetOverridableInitializerNames() multiple times with a model with at least one overridable initializer name (ex. onnxruntime\test\testdata\unused_initializer.onnx):

for (size_t i = 0; i < number_of_runs; ++i)
{
    auto temp_names = session.GetOverridableInitializerNames();
}

Before adding the missing line, the allocated memory size increases after each run.
After adding the missing line, the allocated memory size stays the same.

Urgency

No response

Platform

Other / Unknown

OS Version

Custom

ONNX Runtime Installation

Built from Source

ONNX Runtime Version or Commit ID

on the main branch

ONNX Runtime API

C++

Architecture

Other / Unknown

Execution Provider

Other / Unknown

Execution Provider Library Version

No response

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions