Skip to content

Memory leak when exception is thrown in adl_serializer::to_json #3881

Closed
@mjerabek

Description

@mjerabek

Description

Throwing an exception in serializer function after the output json object is already initialized results in a memory leak (detected with both ASan and valgrind):

void adl_serializer<Foo>::to_json(json& j, Foo const& f) {
    j["a"] = 42;
    throw std::runtime_error("error");
}

Reproduction steps

Reproducer here: https://godbolt.org/z/dvorMExWh

Expected vs. actual results

Expected: no memory leak

Minimal code example

No response

Error messages

No response

Compiler and operating system

linux, clang 15, gcc 12.2

Library version

3.11.2, godbolt trunk, 7f72eed

Validation

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: bugsolution: proposed fixa fix for the issue has been proposed and waits for confirmation

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions