Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Will there still be a memory leak if I don't store the returned ptr?

I was reading this question, and here the jsoncpp CharReaderBuilder::newCharReader() function returns a pointer to a dynamically created CharReader object, which can then be used to parse a JSON.

I understand in that question the OP should have freed the returned pointer once it was used, since it was created on the heap. But, I am confused about whether, if we never store the returned pointer since it is for 1-time-use only, and write something like this:

Json::Value root;
Json::CharReaderBuilder().newCharReader()->parse(serverResponse.response,
    serverResponse.response + serverResponse.size - 1, &root, &Json::String());

Will this still cause a memory leak? If so, then in this case, I am not storing the pointer, so I cannot really call free() or delete to free that location of memory since I need a reference for that.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I guess I could somehow wrap the entire thing around the C++ unique_ptr thing, but I think this should not cause any memory leak. Am I correct?

Edit: From the comments, it seems this will still cause a memory leak. So, how should I delete this pointer since I currently have no reference to it? Am I forced to create a reference and store the pointer if I want to avoid a memory leak? Is there no other way?

>Solution :

"The other way" is using smart pointers. Consider the following examples.

#include <iostream>

struct A {
    int b;
  
    A(int _b) :b(_b) { std::cout << "A created." << std::endl; }
    ~A() { std::cout << "A destroyed." << std::endl; }
};

void c(A *a) {
    std::cout << a->b << std::endl; 
}

int main() {
    c(new A(42));

    return 0;
}

Running this:

% ./a.out
A created.
42

That temporary object created is never cleaned up.

#include <iostream>
#include <memory>

struct A {
    int b;
  
    A(int _b) :b(_b) { std::cout << "A created." << std::endl; }
    ~A() { std::cout << "A destroyed." << std::endl; }
};

void c(std::unique_ptr<A> a) {
    std::cout << a->b << std::endl; 
}

int main() {
    c(std::make_unique<A>(27));

    return 0;
}

Here we’ve use a smart pointer (std::unique_ptr), and we can see that the destructor is called and the temporary is cleaned up.

% ./a.out
A created.
27
A destroyed.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading