Skip to content

Potential memory leak detected by Valgrind #1713

Closed
@muttleyxd

Description

  • What is the issue you have?
    I encountered a weird memory leak in my code, when returning nlohmann::json object from my function, if function returning the object (of any type) throws then 32 bytes are leaked.
    This only happens on GCC, doesn't happen on Clang.

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?
    Code:
    json-valgrind.cpp

#include <nlohmann/json.hpp>

int throw_error()
{
  throw std::runtime_error("leak memory");
}

nlohmann::json minimal_leak()
{
  return {{"leak", throw_error()}};
}

int main()
{
  try {
    minimal_leak();
  } catch(...) { }
  return 0;
}

build:

g++ json-valgrind.cpp -o json-valgrind -g -O0

run:

valgrind ./json_valgrind
  • What is the expected behavior?
    Valgrind exits saying that definitely lost: 0 bytes in 0 blocks

  • And what is the actual behavior instead?
    Valgrind exits saying that definitely lost: 32 bytes in 1 blocks

  • Which compiler and operating system are you using? Is it a supported compiler?
    Tested on Manjaro Linux with:

    • GCC 9.1.0
    • GCC 8.3.0
    • Ubuntu 18.04 Docker container with GCC 7.4.0
  • Did you use a released version of the library or the version from the develop branch?
    Tested with release 3.7.0 and 3.5.0

I wonder if this is nlohmann/json bug or GCC bug, since it doesn't happen on Clang

Metadata

Assignees

No one assigned

    Labels

    kind: bugsolution: proposed fixa fix for the issue has been proposed and waits for confirmationstate: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions