-
December 29th, 2015, 04:36 AM
#1
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];
};
-
December 29th, 2015, 03:29 PM
#2
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.
-
December 30th, 2015, 12:56 AM
#3
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
-
January 5th, 2016, 04:49 AM
#4
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.
-
January 5th, 2016, 08:36 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|