Leaking Singleton

This is a discussion on Leaking Singleton within the C++ Programming forums, part of the General Programming Boards category; Does the CRT leak detection facilities of MSVC++ 2005 Express erroneously report leaks on static pointers allocated with new? When ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412

    Leaking Singleton

    Does the CRT leak detection facilities of MSVC++ 2005 Express erroneously report leaks on static pointers allocated with new?

    When moving my project to this implementation in order to couple it with wxWidgets, I get this one leak on my singleton.

    It's a simple class with one std::vector data member. The constructor uses push_back() to fill the vector. There's nothing weird about this singleton (just a straight no-nonsense implementation of Meyer's).

    MSVC++ reports two leaks. One in the library's xmemory. When I move down on the stack the culprit is found to be the push_back() in the singleton's constructor. This is 78k long (the whole of the vector plus some more, 14060 elements each a long int).

    The second leak (20 bytes long) is what makes me think this is probably a bogus leak report. It's detected inside the getInstance() member function at the point where the static pointer holding the instance gets allocated with new. Now, the language gurantees this pointer gets deleted for me. So, I'm thinking the leak detection facilities don't see past the point where the application exits.

    is this so?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by Mario F. View Post
    Now, the language gurantees this pointer gets deleted for me.
    Why do you think that?

    Anyway... You're worried about a leaking SINGLETON?

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The language guarantees nothing of the kind. An undeleted pointer is an undeleted pointer, and it's the operating system that actually reclaims the memory you leaked.

    But I agree with brewbuck - you're worried about a singleton? As long as it doesn't control any resources that survive process shutdown (shared memory on some systems, for example), don't.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Thanks both.

    I'm worried about a leak report I can't solve or dismiss. However, from your replies I got the impression this memory will indeed be freed when the process shutdowns.

    That is good enough for me. Thanks again.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I guess you can't destroy the singleton itself. Ehm, wait, a singleton has a public destructor, it's just the constructor and copy-operator that is private, right? So you should be able to delete the singleton at program exit. (Assuming of course you can get to it and determine that the singleton isn't active any longer, etc, etc - a reference count or some such can help here).

    --
    Mats

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Are you specifically deleting the objects in the vector? Just calling vector.clear() will not do the trick. This will leak everything in the vector. If you have pointers in the vector you must delete them.

    If the CRT is showing leaks then you certainly have leaks. You can also use _CrtDumpMemoryLeaks() which will tell you if you have any leaks. There are many more CRT memory functions in the compiler help file that you can use to diagnose memory issues.

    The only odd leak I've ever seen was when I was using MFC and it said an MFC DLL failed to unload and memory was leaked. Other than that every leak MSVC has reported for me has been a definite leak. I never did figure out the MFC DLL issue and it went away after I patched the compiler.

    I'm worried about a leak report I can't solve or dismiss. However, from your replies I got the impression this memory will indeed be freed when the process shutdowns.
    Only because of the operating system. This does not mean your program is shutting down correctly. It does mean that Windows ensures your program's failure to clean up it's memory will not jeopardize the stability of the operating system - aka. your program won't crash the OS.
    Last edited by VirtualAce; 08-22-2007 at 05:05 AM.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Darn. I don't have the code in here. I'll repost soon enough with the code. However, I can tell you I'm placing static objects, not heap-based. It's an object which constructor does take a pointer that I use to read data into the object, but I don't store this pointer anywhere...

    I can pretty much guarantee this pointer doesn't leak. I traced it and it gets destroyed when it should. Also, there's no leak report for this particular pointer. The vector doesn't take ownership of it or even attempts to store it. The pointer is a SQLite statement object and gets destroyed with an explicit call to a library function.

    The vector that is part of the singleton is declared as std::vector<Maphex> map_; MapHex is an object representing one hexagon which is described as a long int bit array.

    Anyways, I'll repost later today or tomorow.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singleton template problem
    By cloudy in forum C++ Programming
    Replies: 14
    Last Post: 01-11-2009, 04:40 AM
  2. How To Derive from a singleton
    By appleGuy in forum C++ Programming
    Replies: 8
    Last Post: 03-24-2007, 01:55 PM
  3. Templated singleton classes
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 07-01-2006, 04:06 AM
  4. Thread-safe singleton.
    By Hulag in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2006, 10:45 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21