fix some memory leak

This commit is contained in:
honghui 2014-11-26 14:58:23 +08:00
parent 0e73301fc4
commit 52780dff28
11 changed files with 150 additions and 65 deletions

View File

@ -2,6 +2,7 @@
#include "CCLuaEngine.h" #include "CCLuaEngine.h"
#include "SimpleAudioEngine.h" #include "SimpleAudioEngine.h"
#include "cocos2d.h" #include "cocos2d.h"
#include "CodeIDESupport.h"
#include "Runtime.h" #include "Runtime.h"
#include "ConfigParser.h" #include "ConfigParser.h"
#include "lua_module_register.h" #include "lua_module_register.h"
@ -18,6 +19,13 @@ AppDelegate::AppDelegate()
AppDelegate::~AppDelegate() AppDelegate::~AppDelegate()
{ {
SimpleAudioEngine::end(); SimpleAudioEngine::end();
#if (COCOS2D_DEBUG > 0 && CC_CODE_IDE_DEBUG_SUPPORT > 0)
// NOTE:Please don't remove this call if you want to debug with Cocos Code IDE
endRuntime();
#endif
ConfigParser::purge();
} }
//if you want a different context,just modify the value of glContextAttrs //if you want a different context,just modify the value of glContextAttrs
@ -33,7 +41,7 @@ void AppDelegate::initGLContextAttrs()
bool AppDelegate::applicationDidFinishLaunching() bool AppDelegate::applicationDidFinishLaunching()
{ {
#if (COCOS2D_DEBUG > 0) #if (COCOS2D_DEBUG > 0 && CC_CODE_IDE_DEBUG_SUPPORT > 0)
// NOTE:Please don't remove this call if you want to debug with Cocos Code IDE // NOTE:Please don't remove this call if you want to debug with Cocos Code IDE
initRuntime(); initRuntime();
#endif #endif
@ -69,7 +77,7 @@ bool AppDelegate::applicationDidFinishLaunching()
//LuaStack* stack = engine->getLuaStack(); //LuaStack* stack = engine->getLuaStack();
//register_custom_function(stack->getLuaState()); //register_custom_function(stack->getLuaState());
#if (COCOS2D_DEBUG > 0) #if (COCOS2D_DEBUG > 0 && CC_CODE_IDE_DEBUG_SUPPORT > 0)
// NOTE:Please don't remove this call if you want to debug with Cocos Code IDE // NOTE:Please don't remove this call if you want to debug with Cocos Code IDE
startRuntime(); startRuntime();
#else #else

View File

@ -12,15 +12,20 @@
#define WIN_HEIGHT 640 #define WIN_HEIGHT 640
// ConfigParser // ConfigParser
ConfigParser *ConfigParser::s_sharedInstance = NULL; ConfigParser *ConfigParser::s_sharedConfigParserInstance = NULL;
ConfigParser *ConfigParser::getInstance(void) ConfigParser *ConfigParser::getInstance(void)
{ {
if (!s_sharedInstance) if (!s_sharedConfigParserInstance)
{ {
s_sharedInstance = new ConfigParser(); s_sharedConfigParserInstance = new ConfigParser();
s_sharedInstance->readConfig(); s_sharedConfigParserInstance->readConfig();
} }
return s_sharedInstance; return s_sharedConfigParserInstance;
}
void ConfigParser::purge()
{
CC_SAFE_DELETE(s_sharedConfigParserInstance);
} }
void ConfigParser::readConfig() void ConfigParser::readConfig()

View File

@ -27,6 +27,7 @@ class ConfigParser
{ {
public: public:
static ConfigParser *getInstance(void); static ConfigParser *getInstance(void);
static void purge();
// predefined screen size // predefined screen size
int getScreenSizeCount(void); int getScreenSizeCount(void);
@ -43,7 +44,7 @@ public:
private: private:
void readConfig(); void readConfig();
ConfigParser(void); ConfigParser(void);
static ConfigParser *s_sharedInstance; static ConfigParser *s_sharedConfigParserInstance;
ScreenSizeArray _screenSizeArray; ScreenSizeArray _screenSizeArray;
cocos2d::Size _initViewSize; cocos2d::Size _initViewSize;
string _viewName; string _viewName;

View File

@ -35,18 +35,18 @@ ConnectWaitLayer::ConnectWaitLayer()
{ {
int designWidth = 1280; int designWidth = 1280;
int designHeight = 800; int designHeight = 800;
Image* imagebg = new Image(); _imagebg = new Image();
if (ConfigParser::getInstance()->isLanscape()) if (ConfigParser::getInstance()->isLanscape())
{ {
imagebg->initWithImageData(__landscapePngData, sizeof(__landscapePngData)); _imagebg->initWithImageData(__landscapePngData, sizeof(__landscapePngData));
Director::getInstance()->getOpenGLView()->setDesignResolutionSize(designWidth, designHeight, ResolutionPolicy::EXACT_FIT); Director::getInstance()->getOpenGLView()->setDesignResolutionSize(designWidth, designHeight, ResolutionPolicy::EXACT_FIT);
} else } else
{ {
imagebg->initWithImageData(__portraitPngData, sizeof(__portraitPngData)); _imagebg->initWithImageData(__portraitPngData, sizeof(__portraitPngData));
Director::getInstance()->getOpenGLView()->setDesignResolutionSize(designHeight, designWidth, ResolutionPolicy::FIXED_HEIGHT); Director::getInstance()->getOpenGLView()->setDesignResolutionSize(designHeight, designWidth, ResolutionPolicy::FIXED_HEIGHT);
} }
Texture2D* texturebg = Director::getInstance()->getTextureCache()->addImage(imagebg, "play_background"); Texture2D* texturebg = Director::getInstance()->getTextureCache()->addImage(_imagebg, "play_background");
auto background = Sprite::createWithTexture(texturebg); auto background = Sprite::createWithTexture(texturebg);
background->setAnchorPoint(Vec2(0.5, 0.5)); background->setAnchorPoint(Vec2(0.5, 0.5));
background->setPosition(VisibleRect::center()); background->setPosition(VisibleRect::center());
@ -57,15 +57,15 @@ ConnectWaitLayer::ConnectWaitLayer()
int portraitY = 500; int portraitY = 500;
int lanscaptX = 902; int lanscaptX = 902;
int lanscaptY = 400; int lanscaptY = 400;
Image* imageplay = new Image(); _imageplay = new Image();
imageplay->initWithImageData(__playEnablePngData, sizeof(__playEnablePngData)); _imageplay->initWithImageData(__playEnablePngData, sizeof(__playEnablePngData));
Texture2D* textureplay = Director::getInstance()->getTextureCache()->addImage(imageplay, "play_enable"); Texture2D* textureplay = Director::getInstance()->getTextureCache()->addImage(_imageplay, "play_enable");
auto playSprite = Sprite::createWithTexture(textureplay); auto playSprite = Sprite::createWithTexture(textureplay);
addChild(playSprite, 9999); addChild(playSprite, 9999);
Image* imageShine = new Image(); _imageShine = new Image();
imageShine->initWithImageData(__shinePngData, sizeof(__shinePngData)); _imageShine->initWithImageData(__shinePngData, sizeof(__shinePngData));
Texture2D* textureShine = Director::getInstance()->getTextureCache()->addImage(imageShine, "shine"); Texture2D* textureShine = Director::getInstance()->getTextureCache()->addImage(_imageShine, "shine");
auto shineSprite = Sprite::createWithTexture(textureShine); auto shineSprite = Sprite::createWithTexture(textureShine);
shineSprite->setOpacity(0); shineSprite->setOpacity(0);
Vector<FiniteTimeAction*> arrayOfActions; Vector<FiniteTimeAction*> arrayOfActions;
@ -141,6 +141,13 @@ ConnectWaitLayer::ConnectWaitLayer()
this->scheduleUpdate(); this->scheduleUpdate();
} }
ConnectWaitLayer::~ConnectWaitLayer()
{
CC_SAFE_DELETE(_imagebg);
CC_SAFE_DELETE(_imageplay);
CC_SAFE_DELETE(_imageShine);
}
// clean up: ignore stdin, stdout and stderr // clean up: ignore stdin, stdout and stderr
void ConnectWaitLayer::update(float fDelta) void ConnectWaitLayer::update(float fDelta)
{ {

View File

@ -31,9 +31,14 @@ class ConnectWaitLayer: public cocos2d::Layer
{ {
public: public:
ConnectWaitLayer(); ConnectWaitLayer();
~ConnectWaitLayer();
void update(float fDelta); void update(float fDelta);
private: private:
cocos2d::Image* _imagebg;
cocos2d::Image* _imageplay;
cocos2d::Image* _imageShine;
cocos2d::Label* _labelUploadFile; cocos2d::Label* _labelUploadFile;
}; };

View File

@ -98,6 +98,24 @@ bool reloadScript(const string& file)
return luaStack->executeString(require.c_str()); return luaStack->executeString(require.c_str());
} }
ConsoleCommand* ConsoleCommand::s_sharedConsoleCommand = nullptr;
ConsoleCommand* ConsoleCommand::getShareInstance()
{
if (s_sharedConsoleCommand == nullptr)
{
s_sharedConsoleCommand = new ConsoleCommand();
}
return s_sharedConsoleCommand;
}
void ConsoleCommand::purge()
{
if (s_sharedConsoleCommand != nullptr)
{
delete s_sharedConsoleCommand;
}
}
void ConsoleCommand::init() void ConsoleCommand::init()
{ {
cocos2d::Console *_console = Director::getInstance()->getConsole(); cocos2d::Console *_console = Director::getInstance()->getConsole();
@ -124,12 +142,6 @@ void ConsoleCommand::init()
_fileserver->readResFileFinfo(); _fileserver->readResFileFinfo();
} }
ConsoleCommand::~ConsoleCommand()
{
Director::getInstance()->getConsole()->stop();
_fileserver->stop();
}
void ConsoleCommand::onSendCommand(int fd, const std::string &args) void ConsoleCommand::onSendCommand(int fd, const std::string &args)
{ {
Director::getInstance()->getScheduler()->performFunctionInCocosThread([=](){ Director::getInstance()->getScheduler()->performFunctionInCocosThread([=](){
@ -284,3 +296,8 @@ void ConsoleCommand::onSendCommand(int fd, const std::string &args)
} }
}); });
} }
ConsoleCommand::~ConsoleCommand()
{
Director::getInstance()->getConsole()->stop();
}

View File

@ -29,11 +29,16 @@ THE SOFTWARE.
class ConsoleCommand class ConsoleCommand
{ {
static ConsoleCommand *s_sharedConsoleCommand;
public: public:
static ConsoleCommand* getShareInstance();
static void purge();
void init(); void init();
~ConsoleCommand();
void onSendCommand(int fd, const std::string &args); void onSendCommand(int fd, const std::string &args);
private: private:
~ConsoleCommand();
FileServer* _fileserver; FileServer* _fileserver;
}; };

View File

@ -41,6 +41,20 @@ USING_NS_CC;
#define PROTO_START "RuntimeSend:" #define PROTO_START "RuntimeSend:"
FileServer* FileServer::s_sharedFileServer = nullptr; FileServer* FileServer::s_sharedFileServer = nullptr;
FileServer* FileServer::getShareInstance()
{
if (s_sharedFileServer == nullptr)
{
s_sharedFileServer = new FileServer;
}
return s_sharedFileServer;
}
void FileServer::purge()
{
CC_SAFE_DELETE(s_sharedFileServer);
}
void FileServer::readResFileFinfo() void FileServer::readResFileFinfo()
{ {
std::string filecfg = _writePath + "/fileinfo_debug.json"; std::string filecfg = _writePath + "/fileinfo_debug.json";
@ -177,7 +191,7 @@ bool FileServer::listenOnTCP(int port)
} }
freeaddrinfo(ressave); freeaddrinfo(ressave);
_listenfd = listenfd; _listenfd = listenfd;
_receiveThread = std::thread( std::bind( &FileServer::loopReceiveFile, this) ); _receiveThread = std::thread(std::bind( &FileServer::loopReceiveFile, this));
_writeThread = std::thread(std::bind(&FileServer::loopWriteFile, this)); _writeThread = std::thread(std::bind(&FileServer::loopWriteFile, this));
_responseThread = std::thread(std::bind(&FileServer::loopResponse, this)); _responseThread = std::thread(std::bind(&FileServer::loopResponse, this));
return true; return true;
@ -185,20 +199,34 @@ bool FileServer::listenOnTCP(int port)
void FileServer::stop() void FileServer::stop()
{ {
if(_running) _receiveEndThread = true;
_writeEndThread = true;
_responseEndThread = true;
if(_receiveRunning)
{ {
_endThread = true;
_receiveThread.join(); _receiveThread.join();
_writeThread.join();
_responseThread.join();
} }
if (_writeRunning)
{
_writeThread.join();
}
if (_responseRunning)
{
_responseThread.join();
}
} }
FileServer::FileServer() : FileServer::FileServer() :
_listenfd(-1), _listenfd(-1),
_running(false), _receiveRunning(false),
_endThread(false), _receiveEndThread(false),
_protoBuf(nullptr) _writeRunning(false),
_writeEndThread(false),
_responseRunning(false),
_responseEndThread(false)
{ {
_writePath = FileUtils::getInstance()->getWritablePath(); _writePath = FileUtils::getInstance()->getWritablePath();
@ -218,7 +246,7 @@ _protoBuf(nullptr)
FileServer::~FileServer() FileServer::~FileServer()
{ {
CC_SAFE_DELETE_ARRAY(_protoBuf); stop();
} }
void FileServer::loopReceiveFile() void FileServer::loopReceiveFile()
@ -229,12 +257,9 @@ void FileServer::loopReceiveFile()
/* new client */ /* new client */
client_len = sizeof(client); client_len = sizeof(client);
int fd = accept(_listenfd, (struct sockaddr *)&client, &client_len ); int fd = accept(_listenfd, (struct sockaddr *)&client, &client_len );
if (_protoBuf == nullptr) char *protoBuf = new char[MAXPROTOLENGTH];
{
_protoBuf = new char[MAXPROTOLENGTH];
}
while(!_endThread) { while(!_receiveEndThread) {
// recv start flag // recv start flag
char startflag[13] = {0}; char startflag[13] = {0};
@ -260,12 +285,12 @@ void FileServer::loopReceiveFile()
recvBuf(fd, protolength.char_type, sizeof(protolength.char_type) - 1); recvBuf(fd, protolength.char_type, sizeof(protolength.char_type) - 1);
//recv variable length //recv variable length
memset(_protoBuf, 0, MAXPROTOLENGTH); memset(protoBuf, 0, MAXPROTOLENGTH);
recvBuf(fd, _protoBuf, protolength.uint16_type); recvBuf(fd, protoBuf, protolength.uint16_type);
RecvBufStruct recvDataBuf; RecvBufStruct recvDataBuf;
recvDataBuf.fd = fd; recvDataBuf.fd = fd;
recvDataBuf.fileProto.ParseFromString(_protoBuf); recvDataBuf.fileProto.ParseFromString(protoBuf);
if (1 == recvDataBuf.fileProto.package_seq()) if (1 == recvDataBuf.fileProto.package_seq())
{ {
_recvErrorFile = ""; _recvErrorFile = "";
@ -294,14 +319,14 @@ void FileServer::loopReceiveFile()
unsigned long recvLen = MAXPROTOLENGTH; unsigned long recvLen = MAXPROTOLENGTH;
if(recvTotalLen < MAXPROTOLENGTH) if(recvTotalLen < MAXPROTOLENGTH)
recvLen = recvTotalLen; recvLen = recvTotalLen;
memset(_protoBuf, 0, MAXPROTOLENGTH); memset(protoBuf, 0, MAXPROTOLENGTH);
unsigned long result = recv(fd, _protoBuf, recvLen,0); unsigned long result = recv(fd, protoBuf, recvLen,0);
if (result <= 0) if (result <= 0)
{ {
usleep(1); usleep(1);
continue; continue;
} }
memcpy(contentbuf + contentSize - recvTotalLen, _protoBuf, result); memcpy(contentbuf + contentSize - recvTotalLen, protoBuf, result);
recvTotalLen -= result; recvTotalLen -= result;
} }
@ -328,11 +353,16 @@ void FileServer::loopReceiveFile()
_recvBufListMutex.unlock(); _recvBufListMutex.unlock();
} }
} }
_receiveRunning = false;
CC_SAFE_DELETE_ARRAY(protoBuf);
} }
void FileServer::loopWriteFile() void FileServer::loopWriteFile()
{ {
while(!_endThread) _writeRunning = true;
while(!_writeEndThread)
{ {
_recvBufListMutex.lock(); _recvBufListMutex.lock();
size_t recvSize = _recvBufList.size(); size_t recvSize = _recvBufList.size();
@ -392,6 +422,8 @@ void FileServer::loopWriteFile()
addResponse(recvDataBuf.fd, filename, runtime::FileSendComplete::RESULTTYPE::FileSendComplete_RESULTTYPE_SUCCESS, 0); addResponse(recvDataBuf.fd, filename, runtime::FileSendComplete::RESULTTYPE::FileSendComplete_RESULTTYPE_SUCCESS, 0);
} }
} }
_writeRunning = false;
} }
void FileServer::addResponse(int fd, std::string filename, int errortype, int errornum) void FileServer::addResponse(int fd, std::string filename, int errortype, int errornum)
@ -424,7 +456,8 @@ void FileServer::addResponse(int fd, std::string filename, int errortype, int er
void FileServer::loopResponse() void FileServer::loopResponse()
{ {
while(!_endThread) { _responseRunning = true;
while(!_responseEndThread) {
_responseBufListMutex.lock(); _responseBufListMutex.lock();
size_t responseSize = _responseBufList.size(); size_t responseSize = _responseBufList.size();
_responseBufListMutex.unlock(); _responseBufListMutex.unlock();
@ -463,6 +496,8 @@ void FileServer::loopResponse()
sendBuf(responseBuf.fd, dataBuf, sizeof(responseHeader) + responseString.size()); sendBuf(responseBuf.fd, dataBuf, sizeof(responseHeader) + responseString.size());
cocos2d::log("responseFile:%s,result:%d", fileSendProtoComplete.file_name().c_str(), fileSendProtoComplete.result()); cocos2d::log("responseFile:%s,result:%d", fileSendProtoComplete.file_name().c_str(), fileSendProtoComplete.result());
} }
_responseRunning = false;
} }
bool createDir(const char *sPathName) bool createDir(const char *sPathName)

View File

@ -57,14 +57,9 @@ class FileServer
{ {
static FileServer *s_sharedFileServer; static FileServer *s_sharedFileServer;
public: public:
static FileServer* getShareInstance() static FileServer* getShareInstance();
{ static void purge();
if (s_sharedFileServer == nullptr)
{
s_sharedFileServer = new FileServer();
}
return s_sharedFileServer;
}
bool listenOnTCP(int port); bool listenOnTCP(int port);
void stop(); void stop();
@ -106,15 +101,15 @@ private:
// file descriptor: socket, console, etc. // file descriptor: socket, console, etc.
int _listenfd; int _listenfd;
int _maxfd;
std::vector<int> _fds;
std::thread _responseThread; std::thread _responseThread;
std::thread _receiveThread; std::thread _receiveThread;
std::thread _writeThread; std::thread _writeThread;
fd_set _read_set; bool _receiveRunning;
bool _running; bool _receiveEndThread;
bool _endThread; bool _writeRunning;
char *_protoBuf; bool _writeEndThread;
bool _responseRunning;
bool _responseEndThread;
std::list<RecvBufStruct> _recvBufList; std::list<RecvBufStruct> _recvBufList;
std::list<ResponseStruct> _responseBufList; std::list<ResponseStruct> _responseBufList;

View File

@ -200,7 +200,7 @@ int lua_cocos2dx_runtime_setSearchPaths(lua_State* tolua_S)
return 0; return 0;
std::vector<std::string> originPath; // for IOS platform. std::vector<std::string> originPath; // for IOS platform.
std::vector<std::string> projPath; // for Desktop platform. std::vector<std::string> projPath; // for Desktop platform.
for (int i = 0; i < vecPaths.size(); i++) for (size_t i = 0; i < vecPaths.size(); i++)
{ {
if (!FileUtils::getInstance()->isAbsolutePath(vecPaths[i])) if (!FileUtils::getInstance()->isAbsolutePath(vecPaths[i]))
{ {
@ -269,9 +269,7 @@ void initRuntime()
ScriptEngineManager::getInstance()->setScriptEngine(engine); ScriptEngineManager::getInstance()->setScriptEngine(engine);
register_runtime_override_function(engine->getLuaStack()->getLuaState()); register_runtime_override_function(engine->getLuaStack()->getLuaState());
static ConsoleCommand *g_customCommand; ConsoleCommand::getShareInstance()->init();
g_customCommand = new ConsoleCommand();
g_customCommand->init();
} }
void startRuntime() void startRuntime()
@ -283,3 +281,10 @@ void startRuntime()
scene->addChild(connectLayer); scene->addChild(connectLayer);
director->runWithScene(scene); director->runWithScene(scene);
} }
void endRuntime()
{
ConsoleCommand::purge();
FileServer::getShareInstance()->stop();
//FileServer::purge();
}

View File

@ -43,5 +43,7 @@ void initRuntime();
void startRuntime(); void startRuntime();
void endRuntime();
#endif // _RUNTIME__H_ #endif // _RUNTIME__H_