Skip to content

Fix memory leak in GetOverridableInitializerNames()#29616

Draft
skottmckay with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-cpp-memory-leak
Draft

Fix memory leak in GetOverridableInitializerNames()#29616
skottmckay with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-cpp-memory-leak

Conversation

Copilot AI commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Description

ConstSessionImpl::GetOverridableInitializerNames() was leaking the allocator-owned char* buffer after copying it into std::string, unlike the sibling functions GetInputNames() and GetOutputNames() which correctly call allocator.Free(name).

Fix: Add the missing allocator.Free(name) after emplace_back:

// Before
for (size_t i = 0; i < num_initializers; ++i) {
    char* name;
    ThrowOnError(GetApi().SessionGetOverridableInitializerName(this->p_, i, allocator, &name));
    initializer_names.emplace_back(name);  // name copied, but buffer never freed
}

// After
for (size_t i = 0; i < num_initializers; ++i) {
    char* name;
    ThrowOnError(GetApi().SessionGetOverridableInitializerName(this->p_, i, allocator, &name));
    initializer_names.emplace_back(name);
    allocator.Free(name);  // matches pattern in GetInputNames() / GetOutputNames()
}

Motivation and Context

Every call to GetOverridableInitializerNames() on a model with overridable initializers leaked one heap allocation per initializer. Reproducible by calling the function repeatedly and observing monotonically increasing resident memory.

Add missing allocator.Free(name) after copying the name into the
string vector, matching the pattern used in GetInputNames() and
GetOutputNames().

Fixes #29603
Copilot AI changed the title [WIP] Fix C++ memory leak in GetOverridableInitializerNames Fix memory leak in GetOverridableInitializerNames() Jul 8, 2026
Copilot AI requested a review from skottmckay July 8, 2026 08:21
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.

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

2 participants