修复内存泄露问题

This commit is contained in:
aozhiwei 2019-05-29 14:25:01 +08:00
parent 5a8bad333e
commit c902e2833d
9 changed files with 108 additions and 20 deletions

View File

@ -18,7 +18,7 @@ endif()
set(CMAKE_BUILD_TYPE "Debug") set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_RELEASE "-std=gnu++11 -fsanitize=address -fno-omit-frame-pointer") set(CMAKE_CXX_FLAGS_RELEASE "-std=gnu++11 -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -std=gnu++11") set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -std=gnu++11")
set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -std=gnu++11 -DGAME_ID=${GAME_ID} -DMASTER_MODE=${MASTER_MODE}") set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -std=gnu++11 -DGAME_ID=${GAME_ID} -DMASTER_MODE=${MASTER_MODE} -DA8_TCP_SESSION2=1")
include_directories( include_directories(
AFTER AFTER

View File

@ -71,7 +71,7 @@ static void SavePerfLog()
#endif #endif
} }
void App::Init(int argc, char* argv[]) bool App::Init(int argc, char* argv[])
{ {
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
this->argc = argc; this->argc = argc;
@ -79,10 +79,10 @@ void App::Init(int argc, char* argv[])
if (!ParseOpt()) { if (!ParseOpt()) {
terminated = true; terminated = true;
a8::XPrintf("masterserver启动失败,缺少-i参数\n", {}); a8::XPrintf("wsproxy启动失败,缺少-i参数\n", {});
return; return false;
} }
a8::XPrintf("masterserver starting instance_id:%d pid:%d\n", {instance_id, getpid()}); a8::XPrintf("wsproxy starting instance_id:%d pid:%d\n", {instance_id, getpid()});
loop_mutex_ = new std::mutex(); loop_mutex_ = new std::mutex();
loop_cond_ = new std::condition_variable(); loop_cond_ = new std::condition_variable();
@ -100,7 +100,7 @@ void App::Init(int argc, char* argv[])
MasterSvrMgr::Instance()->Init(); MasterSvrMgr::Instance()->Init();
TargetConnMgr::Instance()->Init(); TargetConnMgr::Instance()->Init();
a8::UdpLog::Instance()->Info("masterserver starting instance_id:%d pid:%d", {instance_id, getpid()}); a8::UdpLog::Instance()->Info("wsproxy starting instance_id:%d pid:%d", {instance_id, getpid()});
{ {
int perf_log_time = 1000 * 60 * 5; int perf_log_time = 1000 * 60 * 5;
if (getenv("is_dev_env")) { if (getenv("is_dev_env")) {
@ -113,13 +113,23 @@ void App::Init(int argc, char* argv[])
SavePerfLog(); SavePerfLog();
}); });
} }
if (HasFlag(1)) {
a8::Timer::Instance()->AddDeadLineTimer(
1000 * 10,
a8::XParams(),
[] (const a8::XParams& param)
{
App::Instance()->terminated = true;
App::Instance()->NotifyLoopCond();
}
);
}
return true;
} }
void App::UnInit() void App::UnInit()
{ {
if (terminated) { a8::XPrintf("wsproxy terminating instance_id:%d pid:%d\n", {instance_id, getpid()});
return;
}
MasterSvrMgr::Instance()->UnInit(); MasterSvrMgr::Instance()->UnInit();
TargetConnMgr::Instance()->UnInit(); TargetConnMgr::Instance()->UnInit();
GameClientMgr::Instance()->UnInit(); GameClientMgr::Instance()->UnInit();
@ -129,6 +139,8 @@ void App::UnInit()
HandlerMgr::Instance()->UnInit(); HandlerMgr::Instance()->UnInit();
UnInitLog(); UnInitLog();
FreeSocketMsgQueue();
FreeIMMsgQueue();
delete im_msg_mutex_; delete im_msg_mutex_;
im_msg_mutex_ = nullptr; im_msg_mutex_ = nullptr;
delete msg_mutex_; delete msg_mutex_;
@ -137,15 +149,13 @@ void App::UnInit()
loop_cond_ = nullptr; loop_cond_ = nullptr;
delete loop_mutex_; delete loop_mutex_;
loop_mutex_ = nullptr; loop_mutex_ = nullptr;
a8::XPrintf("wsproxy terminated instance_id:%d pid:%d\n", {instance_id, getpid()});
} }
int App::Run() int App::Run()
{ {
if (terminated) {
return 0;
}
int ret = 0; int ret = 0;
a8::UdpLog::Instance()->Info("masterserver running", {}); a8::UdpLog::Instance()->Info("wsproxy running", {});
while (!terminated) { while (!terminated) {
a8::tick_t begin_tick = a8::XGetTickCount(); a8::tick_t begin_tick = a8::XGetTickCount();
QuickExecute(); QuickExecute();
@ -476,13 +486,22 @@ void App::UnInitLog()
bool App::ParseOpt() bool App::ParseOpt()
{ {
int ch = 0; int ch = 0;
while ((ch = getopt(argc, argv, "i:")) != -1) { while ((ch = getopt(argc, argv, "i:f:")) != -1) {
switch (ch) { switch (ch) {
case 'i': case 'i':
{ {
instance_id = a8::XValue(optarg); instance_id = a8::XValue(optarg);
} }
break; break;
case 'f':
{
std::vector<std::string> strings;
a8::Split(optarg, strings, ',');
for (auto& str : strings) {
flags.insert(a8::XValue(str).GetInt());
}
}
break;
} }
} }
return instance_id > 0; return instance_id > 0;
@ -504,3 +523,58 @@ a8::XParams* App::GetContext(long long context_id)
auto itr = context_hash_.find(context_id); auto itr = context_hash_.find(context_id);
return itr != context_hash_.end() ? &(itr->second) : nullptr; return itr != context_hash_.end() ? &(itr->second) : nullptr;
} }
bool App::HasFlag(int flag)
{
return flags.find(flag) != flags.end();
}
void App::FreeSocketMsgQueue()
{
msg_mutex_->lock();
if (!work_node_) {
work_node_ = top_node_;
top_node_ = nullptr;
bot_node_ = nullptr;
}
while (work_node_) {
MsgNode* pdelnode = work_node_;
work_node_ = work_node_->next;
if (pdelnode->buf) {
free(pdelnode->buf);
}
free(pdelnode);
if (!work_node_) {
work_node_ = top_node_;
top_node_ = nullptr;
bot_node_ = nullptr;
}
}
msg_mutex_->unlock();
}
void App::FreeIMMsgQueue()
{
im_msg_mutex_->lock();
if (!im_work_node_) {
im_work_node_ = im_top_node_;
im_top_node_ = nullptr;
im_bot_node_ = nullptr;
}
while (im_work_node_) {
IMMsgNode* pdelnode = im_work_node_;
im_work_node_ = im_work_node_->next;
if (pdelnode->msgid == f8::IM_SysMsgQueue) {
a8::XParams* param = (a8::XParams*)pdelnode->params.param1.GetUserData();
delete param;
}
delete pdelnode;
if (!im_work_node_) {
im_work_node_ = im_top_node_;
im_top_node_ = nullptr;
im_bot_node_ = nullptr;
}
}
im_msg_mutex_->unlock();
}

View File

@ -12,7 +12,7 @@ class App : public a8::Singleton<App>
public: public:
void Init(int argc, char* argv[]); bool Init(int argc, char* argv[]);
void UnInit(); void UnInit();
int Run(); int Run();
@ -31,6 +31,7 @@ class App : public a8::Singleton<App>
a8::XParams* AddContext(long long context_id); a8::XParams* AddContext(long long context_id);
void DelContext(long long context_id); void DelContext(long long context_id);
a8::XParams* GetContext(long long context_id); a8::XParams* GetContext(long long context_id);
bool HasFlag(int flag);
private: private:
void QuickExecute(); void QuickExecute();
@ -49,11 +50,14 @@ private:
void UnInitLog(); void UnInitLog();
bool ParseOpt(); bool ParseOpt();
void FreeSocketMsgQueue();
void FreeIMMsgQueue();
public: public:
int argc = 0; int argc = 0;
char** argv = nullptr; char** argv = nullptr;
volatile bool terminated = false; volatile bool terminated = false;
std::set<int> flags;
PerfMonitor perf; PerfMonitor perf;
a8::uuid::SnowFlake uuid; a8::uuid::SnowFlake uuid;

View File

@ -14,6 +14,10 @@ void GameClientMgr::Init()
void GameClientMgr::UnInit() void GameClientMgr::UnInit()
{ {
for (auto& pair : socket_hash_) {
delete pair.second;
}
socket_hash_.clear();
} }
void GameClientMgr::OnClientDisconnect(a8::XParams& param) void GameClientMgr::OnClientDisconnect(a8::XParams& param)

View File

@ -4,8 +4,9 @@
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
int exitcode = 0; int exitcode = 0;
App::Instance()->Init(argc, argv); if (App::Instance()->Init(argc, argv)) {
exitcode = App::Instance()->Run(); exitcode = App::Instance()->Run();
App::Instance()->UnInit(); App::Instance()->UnInit();
}
return exitcode; return exitcode;
} }

View File

@ -94,6 +94,7 @@ void TargetConn::SendStockMsg()
void TargetConn::ForwardClientMsg(f8::MsgHdr& hdr) void TargetConn::ForwardClientMsg(f8::MsgHdr& hdr)
{ {
char* buff = (char*)malloc(sizeof(f8::WSProxyPackHead_C) + hdr.buflen); char* buff = (char*)malloc(sizeof(f8::WSProxyPackHead_C) + hdr.buflen);
memset(buff, 0, sizeof(f8::WSProxyPackHead_C));
f8::WSProxyPackHead_C* head = (f8::WSProxyPackHead_C*)buff; f8::WSProxyPackHead_C* head = (f8::WSProxyPackHead_C*)buff;
head->packlen = hdr.buflen; head->packlen = hdr.buflen;
head->msgid = hdr.msgid; head->msgid = hdr.msgid;

View File

@ -26,6 +26,10 @@ void TargetConnMgr::Init()
void TargetConnMgr::UnInit() void TargetConnMgr::UnInit()
{ {
for (auto& pair : id_hash_) {
pair.second->UnInit();
delete pair.second;
}
} }
TargetConn* TargetConnMgr::GetConnByKey(const std::string& key) TargetConn* TargetConnMgr::GetConnByKey(const std::string& key)

@ -1 +1 @@
Subproject commit 07650796618d3d5f86bc3749e817bdc4848458b9 Subproject commit 0ae4192e3a75e5627a80e5131fb4425948f146b6

@ -1 +1 @@
Subproject commit 0d1fd33446c6dda16e8ecb5a0ad9b0e42c44deee Subproject commit 7af5d3b84aeaa84a2979c24db9db5ed5a43cea2d