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 "SimpleAudioEngine.h"
#include "cocos2d.h"
#include "CodeIDESupport.h"
#include "Runtime.h"
#include "ConfigParser.h"
#include "lua_module_register.h"
@ -18,6 +19,13 @@ AppDelegate::AppDelegate()
AppDelegate::~AppDelegate()
{
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
@ -33,7 +41,7 @@ void AppDelegate::initGLContextAttrs()
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
initRuntime();
#endif
@ -69,7 +77,7 @@ bool AppDelegate::applicationDidFinishLaunching()
//LuaStack* stack = engine->getLuaStack();
//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
startRuntime();
#else

View File

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

View File

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

View File

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

View File

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

View File

@ -98,6 +98,24 @@ bool reloadScript(const string& file)
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()
{
cocos2d::Console *_console = Director::getInstance()->getConsole();
@ -124,12 +142,6 @@ void ConsoleCommand::init()
_fileserver->readResFileFinfo();
}
ConsoleCommand::~ConsoleCommand()
{
Director::getInstance()->getConsole()->stop();
_fileserver->stop();
}
void ConsoleCommand::onSendCommand(int fd, const std::string &args)
{
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
{
static ConsoleCommand *s_sharedConsoleCommand;
public:
static ConsoleCommand* getShareInstance();
static void purge();
void init();
~ConsoleCommand();
void onSendCommand(int fd, const std::string &args);
private:
~ConsoleCommand();
FileServer* _fileserver;
};

View File

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

View File

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

View File

@ -200,7 +200,7 @@ int lua_cocos2dx_runtime_setSearchPaths(lua_State* tolua_S)
return 0;
std::vector<std::string> originPath; // for IOS 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]))
{
@ -269,9 +269,7 @@ void initRuntime()
ScriptEngineManager::getInstance()->setScriptEngine(engine);
register_runtime_override_function(engine->getLuaStack()->getLuaState());
static ConsoleCommand *g_customCommand;
g_customCommand = new ConsoleCommand();
g_customCommand->init();
ConsoleCommand::getShareInstance()->init();
}
void startRuntime()
@ -283,3 +281,10 @@ void startRuntime()
scene->addChild(connectLayer);
director->runWithScene(scene);
}
void endRuntime()
{
ConsoleCommand::purge();
FileServer::getShareInstance()->stop();
//FileServer::purge();
}

View File

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