Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak: Json::Value cannot be release #1548

Open
OnlySoaring opened this issue Jul 8, 2024 · 4 comments
Open

Memory leak: Json::Value cannot be release #1548

OnlySoaring opened this issue Jul 8, 2024 · 4 comments

Comments

@OnlySoaring
Copy link

OnlySoaring commented Jul 8, 2024

Describe the bug
When I use Json:: Reader or Json:: parseFromStream to create Json:: Value, the memory of Json:: Value is not released when the function ends

To Reproduce

int main(int argc, char** argv)
{
  while(true)
  {
    Json::Value root;
    std::ifstream ifs("/opt/user.json");
    Json::Reader reader;
    reader.parse(ifs, root);
    ifs.close();

    std::cout<<"1"<<std::endl;
    break;
  }

  while(true)
  {
    std::cout<<2<<std::endl;
  }
return 0;
}

Expected behavior
My JSON file has 681MB. When the test program printed 1, the program occupied 2.12GB of memory. At the end of "while", Json:: value should be released, but when the test program prints 2, the memory does not decrease. Whether I use Json:: Reader or Json:: parseFromStream, it's the same. This is fatal for multi program projects as it occupies useless memory.

Desktop (please complete the following information):

  • OS: Ubuntu20.04
  • C++ 17
@OnlySoaring
Copy link
Author

Use Json:: Value root or Json:: Value * root=new Json:: Value (); Also unable to release

@OnlySoaring OnlySoaring changed the title Memory leakage: Json::Value cannot be release Memory leak: Json::Value cannot be release Jul 8, 2024
@KRashEk
Copy link

KRashEk commented Sep 5, 2024

I tried this code. However, the code shows a memory leak when you are not handling the signal. If you handle the SIGINT signal try to break the loop using CTRL + C. There is no memory leak. Here is my code. please try this.

#include "jsoncpp/json/json.h"
#include <iostream>
#include <fstream>
#include <signal.h>

bool is_true = true;
void sigint(int signum)
{
is_true = false;
}

int main(int argc, char** argv)
{
signal(SIGINT,sigint);

while(true)
{
Json::Value root;
std::ifstream ifs("/opt/user.json");
Json::Reader reader;
reader.parse(ifs, root);
ifs.close();

`std::cout<<"1"<<std::endl;`
`break;`

}

while(is_true)
{
std::cout<<2<<std::endl;
}
return 0;
}

@daudrain
Copy link

daudrain commented Dec 1, 2024

@OnlySoaring ,

you may try to check to replace Json::Value root; with LocalJsonValue root; where LocalJsonValue is defined like this:

class LocalJsonValue : public Json::Value {
public:
  LocalJsonValue() = default;
  ~LocalJsonValue() {
    std::cout<<"Destruct LocalJsonValue\n";
  }
};

Destruct LocalJsonValue should be printed before 2 is being printed.

Is compilation optimization enabled?

@yunhai2009
Copy link

@OnlySoaring I also encountered this problem。

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

No branches or pull requests

4 participants