From e572df8a55bd4a79a9a7ea7d0b8126e912477fcf Mon Sep 17 00:00:00 2001 From: Wenhai Lin Date: Thu, 14 Apr 2016 23:41:46 +0800 Subject: [PATCH] Fixed memory leak --- native/cocos/2d/CCLabelTTF.h | 3 ++- .../cocos/editor-support/cocostudio/CocoStudio.cpp | 12 +++++++++++- .../scripting/js-bindings/manual/ScriptingCore.cpp | 6 ++++++ .../js-bindings/manual/network/XMLHTTPRequest.cpp | 14 ++++++-------- .../GUI/CCControlExtension/CCControlButton.cpp | 13 +++++++++---- 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/native/cocos/2d/CCLabelTTF.h b/native/cocos/2d/CCLabelTTF.h index ea8dd63a9d..332d3b3f64 100644 --- a/native/cocos/2d/CCLabelTTF.h +++ b/native/cocos/2d/CCLabelTTF.h @@ -2,6 +2,7 @@ #define _COCOS2D_CCLABELTTF_H_ #include "2d/CCNode.h" +#include "2d/CCLabel.h" NS_CC_BEGIN @@ -16,7 +17,7 @@ public: setAnchorPoint(Vec2::ANCHOR_MIDDLE); setCascadeColorEnabled(true); setCascadeOpacityEnabled(true); - _renderLabel = new (std::nothrow) Label; + _renderLabel = Label::create(); _renderLabel->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT); addChild(_renderLabel); } diff --git a/native/cocos/editor-support/cocostudio/CocoStudio.cpp b/native/cocos/editor-support/cocostudio/CocoStudio.cpp index 8bf879d9b9..794adeb404 100644 --- a/native/cocos/editor-support/cocostudio/CocoStudio.cpp +++ b/native/cocos/editor-support/cocostudio/CocoStudio.cpp @@ -1,11 +1,21 @@ #include "CocoStudio.h" +#include "Armature/CCArmatureDataManager.h" +#include "Armature/CCDataReaderHelper.h" +#include "Armature/CCSpriteFrameCacheHelper.h" +#include "CCActionManagerEx.h" +#include "DictionaryHelper.h" namespace cocostudio { void destroyCocosStudio() { - + ActionManagerEx::destroyInstance(); + SpriteFrameCacheHelper::purge(); + + ArmatureDataManager::destroyInstance(); + DataReaderHelper::purge(); + DictionaryHelper::destroyInstance(); } } diff --git a/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp b/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp index 5835373f14..0e2a338759 100644 --- a/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp +++ b/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp @@ -823,6 +823,12 @@ ScriptingCore::~ScriptingCore() void ScriptingCore::cleanup() { + if(_runLoop) + { + delete _runLoop; + _runLoop = nullptr; + } + localStorageFree(); if (_cx) diff --git a/native/cocos/scripting/js-bindings/manual/network/XMLHTTPRequest.cpp b/native/cocos/scripting/js-bindings/manual/network/XMLHTTPRequest.cpp index 3e813bf496..a24ff2a5fc 100644 --- a/native/cocos/scripting/js-bindings/manual/network/XMLHTTPRequest.cpp +++ b/native/cocos/scripting/js-bindings/manual/network/XMLHTTPRequest.cpp @@ -191,6 +191,10 @@ void MinXmlHttpRequest::_setHttpRequestData(const char *data, size_t len) void MinXmlHttpRequest::handle_requestResponse(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response) { _elapsedTime = 0; + if (Director::DirectorInstance == nullptr) { + return; + } + Director::DirectorInstance->getScheduler()->unscheduleAllForTarget(this); if(_isAborted || _readyState == UNSENT) @@ -285,8 +289,6 @@ MinXmlHttpRequest::MinXmlHttpRequest() , _requestHeader() , _isAborted(false) { - Director::DirectorInstance->getScheduler()->retain(); - JSContext* cx = ScriptingCore::getInstance()->getGlobalContext(); _onreadystateCallback.construct(cx); _onloadstartCallback.construct(cx); @@ -318,10 +320,6 @@ MinXmlHttpRequest::~MinXmlHttpRequest() } CC_SAFE_FREE(_data); - - if (Director::DirectorInstance) { - Director::DirectorInstance->getScheduler()->release(); - } } /** @@ -783,7 +781,7 @@ JS_BINDED_FUNC_IMPL(MinXmlHttpRequest, send) _sendRequest(cx); _notify(_onloadstartCallback.ref()); //begin schedule for timeout - if(_timeout > 0) + if(_timeout > 0 && Director::DirectorInstance) { Director::DirectorInstance->getScheduler()->scheduleUpdate(this, 0, false); } @@ -795,7 +793,7 @@ JS_BINDED_FUNC_IMPL(MinXmlHttpRequest, send) void MinXmlHttpRequest::update(float dt) { _elapsedTime += dt; - if(_elapsedTime * 1000 >= _timeout) + if(_elapsedTime * 1000 >= _timeout && Director::DirectorInstance) { _notify(_ontimeoutCallback.ref()); _elapsedTime = 0; diff --git a/native/extensions/GUI/CCControlExtension/CCControlButton.cpp b/native/extensions/GUI/CCControlExtension/CCControlButton.cpp index b9d6a93c60..642844ed89 100644 --- a/native/extensions/GUI/CCControlExtension/CCControlButton.cpp +++ b/native/extensions/GUI/CCControlExtension/CCControlButton.cpp @@ -129,10 +129,15 @@ bool ControlButton::initWithLabelAndBackgroundSprite(Node* node, ui::Scale9Sprit ControlButton* ControlButton::create(Node* label, cocos2d::ui::Scale9Sprite* backgroundSprite) { - ControlButton *pRet = new (std::nothrow) ControlButton(); - pRet->initWithLabelAndBackgroundSprite(label, backgroundSprite, true); - pRet->autorelease(); - return pRet; + ControlButton *ret = new (std::nothrow) ControlButton(); + if (ret && ret->initWithLabelAndBackgroundSprite(label, backgroundSprite, true)) + { + ret->autorelease(); + return ret; + } + + delete ret; + return nullptr; } ControlButton* ControlButton::create(Node* label, cocos2d::ui::Scale9Sprite* backgroundSprite, bool adjustBackGroundSize)