cocos2d-x的单例析构时为何没delete操作

想写一个单例,在网上找了下资料,多数都是参考cocos2dx里的单例的,例如Director,代码如下:

// getInstance()

Director* Director::getInstance()
{
if (!s_SharedDirector)
{
s_SharedDirector = new (std::nothrow) DisplayLinkDirector();
CCASSERT(s_SharedDirector, “FATAL: Not enough memory”);
s_SharedDirector->init();
}

return s_SharedDirector;

}

//析构函数

Director::~Director(void)
{
CCLOGINFO(“deallocing Director: %p”, this);

CC_SAFE_RELEASE(_FPSLabel);
CC_SAFE_RELEASE(_drawnVerticesLabel);

// …中间省略…

s_SharedDirector = nullptr;

}

=========================================================================

这里有点不明白,new和delete是配对使用的,为何用new生成s_SharedDirector后,在析构函数里没用delete,析构函数里只是将指针赋空,这样分配的空间还没得到释放啊?

有人知道吗?

void Configuration::destroyInstance()
{
CC_SAFE_RELEASE_NULL(s_sharedConfiguration);
}

#define CC_SAFE_RELEASE_NULL§ do { if§ { §->release(); § = nullptr; } } while(0)

Configuration::release===>Ref::release

void Ref::release()
{
CCASSERT(_referenceCount > 0, “reference count should greater than 0”);
–_referenceCount;

if (_referenceCount == 0)
{

#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
auto poolManager = PoolManager::getInstance();
if (!poolManager->getCurrentPool()->isClearing() && poolManager->isObjectInPools(this))
{
// Trigger an assert if the reference count is 0 but the Ref is still in autorelease pool.
// This happens when ‘autorelease/release’ were not used in pairs with ‘new/retain’.
//
// Wrong usage (1):
//
// auto obj = Node::create(); // Ref = 1, but it’s an autorelease Ref which means it was in the autorelease pool.
// obj->autorelease(); // Wrong: If you wish to invoke autorelease several times, you should retain obj first.
//
// Wrong usage (2):
//
// auto obj = Node::create();
// obj->release(); // Wrong: obj is an autorelease Ref, it will be released when clearing current pool.
//
// Correct usage (1):
//
// auto obj = Node::create();
// |- new Node(); // new is the pair of the autorelease of next line
// |- autorelease(); // The pair of new Node.
//
// obj->retain();
// obj->autorelease(); // This autorelease is the pair of retain of previous line.
//
// Correct usage (2):
//
// auto obj = Node::create();
// obj->retain();
// obj->release(); // This release is the pair of retain of previous line.
CCASSERT(false, “The reference shouldn’t be 0 because it is still in autorelease pool.”);
}
#endif

#if CC_USE_MEM_LEAK_DETECTION
untrackRef(this);
#endif
delete this;
}
}

看了下Configuration, 好像和s_SharedDirector没什么关系,也许在什么地方进行删除操作,但我没发现

=================================================================

Configuration::Configuration()
: _maxTextureSize(0)
, _maxModelviewStackDepth(0)
, _supportsPVRTC(false)
, _supportsETC1(false)
, _supportsS3TC(false)
, _supportsATITC(false)
, _supportsNPOT(false)
, _supportsBGRA8888(false)
, _supportsDiscardFramebuffer(false)
, _supportsShareableVAO(false)
, _maxSamplesAllowed(0)
, _maxTextureUnits(0)
, _glExtensions(nullptr)
{
}

bool Configuration::init()
{
_valueDict"cocos2d.x.version"] = Value(cocos2dVersion());

#if CC_ENABLE_PROFILERS
_valueDict"cocos2d.x.compiled_with_profiler"] = Value(true);
#else
_valueDict"cocos2d.x.compiled_with_profiler"] = Value(false);
#endif

#if CC_ENABLE_GL_STATE_CACHE == 0
_valueDict"cocos2d.x.compiled_with_gl_state_cache"] = Value(false);
#else
_valueDict"cocos2d.x.compiled_with_gl_state_cache"] = Value(true);
#endif

#if COCOS2D_DEBUG
_valueDict"cocos2d.x.build_type"] = Value(“DEBUG”);
#else
_valueDict"cocos2d.x.build_type"] = Value(“RELEASE”);
#endif

return true;

}

Configuration::~Configuration()
{
}

Configuration* Configuration::getInstance()
{
if (! s_sharedConfiguration)
{
s_sharedConfiguration = new Configuration();
s_sharedConfiguration->init();
}

return s_sharedConfiguration;

}

void Configuration::destroyInstance()
{
CC_SAFE_RELEASE_NULL(s_sharedConfiguration);
}

============================================================

Configuration, 好像和s_SharedDirector没什么关系=====>

这两个怎么会没有关系呢,我上面的回答把所有调用过程都给你帖出来了呀,只是一个个列,没作文字说明,但是你应该看的到呀。最后有个delete this;

s_sharedConfiguration 就是Configuration的一个实例,你看他是怎么new出来的就知道了!

但我问的是s_SharedDirector,非s_sharedConfiguration,同学你是不是看错了

void Director::purgeDirector()
{
// cleanup scheduler
getScheduler()->unscheduleAll();

// Disable event dispatching
if (_eventDispatcher)
{
    _eventDispatcher->setEnabled(false);
}

if (_runningScene)
{
    _runningScene->onExit();
    _runningScene->cleanup();
    _runningScene->release();
}

_runningScene = nullptr;
_nextScene = nullptr;

// remove all objects, but don't release it.
// runWithScene might be executed after 'end'.
_scenesStack.clear();

stopAnimation();

CC_SAFE_RELEASE_NULL(_FPSLabel);
CC_SAFE_RELEASE_NULL(_drawnBatchesLabel);
CC_SAFE_RELEASE_NULL(_drawnVerticesLabel);

// purge bitmap cache
FontFNT::purgeCachedData();

FontFreeType::shutdownFreeType();

// purge all managed caches
DrawPrimitives::free();
AnimationCache::destroyInstance();
SpriteFrameCache::destroyInstance();
GLProgramCache::destroyInstance();
GLProgramStateCache::destroyInstance();
FileUtils::destroyInstance();

// cocos2d-x specific data structures
UserDefault::destroyInstance();

GL::invalidateStateCache();

destroyTextureCache();

CHECK_GL_ERROR_DEBUG();

// OpenGL view
if (_openGLView)
{
    _openGLView->end();
    _openGLView = nullptr;
}

// delete Director
release();

}

在这释放掉的.。。。。。。。

— Begin quote from ____

引用第5楼pp2k04于2014-10-20 00:53发表的 回 4楼(zhouciming) 的帖子 :
但我问的是s_SharedDirector,非s_sharedConfiguration,同学你是不是看错了 http://www.cocoachina.com/bbs/job.php?action=topost&tid=235230&pid=1078601

— End quote

不好意思啊,我还真是看错了!:3:

— Begin quote from ____

引用第6楼军军于2014-10-20 01:29发表的 :
void Director::purgeDirector()
{
// cleanup scheduler
getScheduler()->unscheduleAll();

http://www.cocoachina.com/bbs/job.php?action=topost&tid=235230&pid=1078606

— End quote

好威武,学习了:14:

谢谢6楼,没细看,没注意到还有这个函数