Skip to content

Possible memory leak on push_back #1971

@davide9

Description

@davide9

I'm experiencing a memory leak when using push_back to add strings to a json object

Steps to reproduce:

  • Use a custom allocator to track memory allocations
  • Create a json object as array: json myJson = json::array_t{};
  • Add a string type object with push_back: myJson.push_back(String{"Hello leaking world"});
  • Destroy the json instance.
  • Experience the leak.

I'm using Visual Studio 2017 on Windows 10, compiling with standard Debug configuration in x64
My latest synch on this branch was on 28/02/2020 at 15:20 CET.

I've (probably) fixed the issue by removing (on the single header file) the line 19426: val.m_type = value_t::null; from the function void push_back(basic_json&& val).

From my debugging, the issue comes from the fact that the m_value.array->push_back(std::move(val)); is invoking the basic_json(const basic_json&) instead of the basic_json(basic_json&&), which creates a copy of the source value that should still be properly delete by the allocator. My fix shouldn't affect the final result since, if the move constructor is invoked in the push_back of 19422, the move constructor will take care of marking the type of the source basic_json as null.

Hope this will be helpful!

Cheers,

Davide.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions