CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    Problem with deletion of Singleton.

    When I the application exits, it calls on the destructor, the destructor in turn calls the
    CShelvingSingleton:estroySingleton(); function, I had a similar Singleton which precedes this call,
    but the last call was successful instead of this failing one.
    Any ideas why?

    Code:
    File: f:\dd\vctools\crt\crtw32\misc\dbgdel.cpp
    Line: 52
    
    Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
    Thanks
    Jack

    Code:
    #ifndef CSHELVING_H_
    #define CSHELVING_H_
    
    #include "enum.h" 
    #include "PathfindingStrategy.h"
    #include "CObject.h"
    #include "Item.h"
    
     
    class CShelving : public Item {
    
    public:
    	CShelving() {
    
    	}
    
    	char GetType() { return T_CARTON; }
    };
    
    class ShelvingSingleton {
    
    	static ShelvingSingleton* _instance;
    
    private:
    	ShelvingSingleton() { }
    
    	static void deleter(ShelvingSingleton *ptr) { delete ptr; }
    
    public:
    	~ShelvingSingleton() {}
    	static void create() {
    		if (_instance == NULL) {
    			_instance = new ShelvingSingleton;
    		}
    	}
    
    	static void DestroySingleton() {
    		delete _instance;
    		_instance = 0;
    	}
    
    	static CShelving* GetShelf(int index) {
    		if (index > NO_CARTONS) {
    			return NULL;
    		}
    
    		return &shelves[index];
    	}
    
    
    	static CShelving shelves[NO_CARTONS];
    };

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Problem with deletion of Singleton.

    Why not call the static destroySingleton method. Also, the deleter method is a bit funky having to pass in a pointer to a singleton. Is that necessary.

  3. #3
    Join Date
    Dec 2010
    Posts
    907

    Re: Problem with deletion of Singleton.

    I am actually calling the DestroySingleton, Sorry for being misleading, I meant I call the destructor of my main application, which in turn
    calling ShelvingSingleton:estroySingleton() which causes the problem.
    Thanks
    Jack

  4. #4
    Join Date
    Aug 2006
    Posts
    231

    Re: Problem with deletion of Singleton.

    If you just follow the CppCoreGuidelines, you will less likely cause invalid heap.

    https://github.com/isocpp/CppCoreGuidelines

    In this case, if you use std::unique_ptr, you don't need to explicitly delete the singleton instance when the application exits.

    Are you sure you need a singleton in the first place? Singletons are rarely needed and often misused. Try to avoid global data as much as you can.

    Also, it seems that the private deleter method is never called. Was it added by mistake?
    Last edited by TubularX; January 5th, 2016 at 04:54 AM.

  5. #5
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Problem with deletion of Singleton.

    Yes I concur with TubularX. Best use smart pointers in this case.
    ahoodin
    To keep the plot moving, that's why.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured