Skip to content

Memory leak when looking up auxiliary_keys with stack_reference in usertype_storage_base::self_index_call #1732

@yangyulai

Description

@yangyulai

Affected code File: include/lua/sol/usertype_storage.hpp Function: usertype_storage_base::self_index_call(...)
Snippet: stateless_reference* target = nullptr; { stack_reference k = stack::get<stack_reference>(L, 2); auto it = self.auxiliary_keys.find(k); if (it != self.auxiliary_keys.cend()) { target = &it->second; } }
Problem auxiliary_keys is std::unordered_map<stateless_reference, ...>. Calling find(k) with k of type stack_reference can implicitly create a temporary stateless_reference via stateless_reference(lua_State*, const stack_reference&), which allocates a new Lua registry reference. Because stateless_reference cannot free in its destructor (no lua_State*), this temporary’s registry ref is never released, causing a leak on each lookup when heterogeneous lookup is not active.
Impact Repeated non-string key lookups (e.g., table/lightuserdata keys) cause unbounded growth of Lua registry entries and memory usage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions