Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7945903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:59:58+00:00 2026-06-04T00:59:58+00:00

I don’t know why I have a memory leak here and would really appreciate

  • 0

I don’t know why I have a memory leak here and would really appreciate any advice.
Notice that before the process terminates I call destroy(), a static member function that is supposed to delete the singleton object.

Here’s the relevant code and valgrind’s messaeg:

Manager.h:
class Manager {
public:
    // Constructor/destructor
    static Manager * instance();
    static void destroy();
    ~Manager();
        // Bunch of functions that I didn't write here

private:
    Manager();
    static Manager * _singleton; 
        // Bunch of fields that I didn't write here
};

Manager.cpp:
#include "Manager.h"

Manager * Manager::_singleton = NULL;

Manager * Manager::instance() {
    if (_singleton == NULL) {
            _singleton = new Manager();
        }
    return _singleton;
}

void Manager::destroy()
{
    delete _singleton;
    _singleton = NULL;
}

/*
 * Destructor
 */
Manager::~Manager() {
    // Deleting all fields here, memory leak is not from a field anyway
}

And here’s valgrind’s report:

==28688== HEAP SUMMARY:
==28688==     in use at exit: 512 bytes in 1 blocks
==28688==   total heap usage: 12 allocs, 11 frees, 10,376 bytes allocated
==28688== 
==28688== 512 bytes in 1 blocks are definitely lost in loss record 1 of 1
==28688==    at 0x4C27297: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==28688==    by 0x4014CE: Manager::Manager() (Manager.cpp:33)
==28688==    by 0x401437: Manager::instance() (Manager.cpp:15)
==28688==    by 0x4064E4: initdevice(char*) (outputdevice.cpp:69)
==28688==    by 0x406141: main (driver.cpp:21)
==28688== 
==28688== LEAK SUMMARY:
==28688==    definitely lost: 512 bytes in 1 blocks
==28688==    indirectly lost: 0 bytes in 0 blocks
==28688==      possibly lost: 0 bytes in 0 blocks
==28688==    still reachable: 0 bytes in 0 blocks
==28688==         suppressed: 0 bytes in 0 blocks

Why do I have this leak? I do delete _singleton in destroy()

As I said I would appreciate any help, thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:59:59+00:00Added an answer on June 4, 2026 at 12:59 am

    There are several things to note here. The most obvious is: when do you
    call Manager::destroy. The second thing is that the memory valgrind
    seems to be complaining about is allocated in the constructor of
    Manager, which you don’t show us. If we can believe the error message
    (and I’ve generally found valgrind to be reliable in this respect,
    although it can be fooled), there is no remaining pointer to the memory;
    either you’re not deleting one of the fields in the object, or the
    object allocates memory in its constructor which it doesn’t save in a
    field.

    Finally, it’s usually better practice not to destruct a singleton.
    The whole purpose of the singleton idiom is to avoid order of
    initialization issues (in C++, at least—otherwise, just declaring
    a static instance of the object is sufficient). Destructing the object
    introduces possible order of destruction issues. If you don’t destruct
    it, valgrind will warn about possible memory leaks (not “definitly
    lost”); you can either ignore the warnings, or filter them out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know if this has been answered before. Have custom routes to users. If
don't know better title for this, but here's my code. I have class user
Don't really know how to formulate the title, but it should be pretty obvious
I don't know whether this is really possible, but I'm trying my best. If
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this has been asked before, so point me to another question
Don't know what's wrong here, when I run the application it says Specified method
DON'T ASK WHY but... I have a regex that needs to be case insensitive
I don't know if this has been asked before, but what i'd like to
I don't have any experience in updating a Rails app and when I google

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.