From 301e8e7019cbe0b24fcf7f25db0d96389dd57d2b Mon Sep 17 00:00:00 2001 From: thunder2 Date: Sun, 25 Jan 2015 23:22:58 +0000 Subject: [PATCH] Fixed memory leak in request handling by adding a destructor the the request classes derived from GxsRequest and delete the result. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@7876 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/gxs/rsgxsdataaccess.cc | 9 +++ libretroshare/src/gxs/rsgxsrequesttypes.cc | 61 +++++++++++++++++++ libretroshare/src/gxs/rsgxsrequesttypes.h | 44 ++++++------- libretroshare/src/libretroshare.pro | 7 ++- .../src/retroshare/rsgxsifacehelper.h | 2 +- libretroshare/src/util/rsstd.h | 37 +++++++++++ 6 files changed, 135 insertions(+), 25 deletions(-) create mode 100644 libretroshare/src/gxs/rsgxsrequesttypes.cc create mode 100644 libretroshare/src/util/rsstd.h diff --git a/libretroshare/src/gxs/rsgxsdataaccess.cc b/libretroshare/src/gxs/rsgxsdataaccess.cc index 4392615bb..bed0366e6 100644 --- a/libretroshare/src/gxs/rsgxsdataaccess.cc +++ b/libretroshare/src/gxs/rsgxsdataaccess.cc @@ -412,6 +412,7 @@ bool RsGxsDataAccess::getGroupSummary(const uint32_t& token, std::listmGroupMetaData; + gmreq->mGroupMetaData.clear(); locked_updateRequestStatus(token, GXS_REQUEST_V2_STATUS_DONE); }else{ std::cerr << "RsGxsDataAccess::getGroupSummary() Req found, failed caste" << std::endl; @@ -442,6 +443,7 @@ bool RsGxsDataAccess::getGroupData(const uint32_t& token, std::list& if(gmreq) { grpData = gmreq->mGroupData; + gmreq->mGroupData.clear(); locked_updateRequestStatus(token, GXS_REQUEST_V2_STATUS_DONE); }else{ std::cerr << "RsGxsDataAccess::getGroupData() Req found, failed caste" << std::endl; @@ -473,6 +475,7 @@ bool RsGxsDataAccess::getMsgData(const uint32_t& token, NxsMsgDataResult& msgDat if(mdreq) { msgData = mdreq->mMsgData; + mdreq->mMsgData.clear(); locked_updateRequestStatus(token, GXS_REQUEST_V2_STATUS_DONE); } else @@ -509,6 +512,7 @@ bool RsGxsDataAccess::getMsgRelatedData(const uint32_t &token, NxsMsgRelatedData if(mrireq) { msgData = mrireq->mMsgDataResult; + mrireq->mMsgDataResult.clear(); locked_updateRequestStatus(token, GXS_REQUEST_V2_STATUS_DONE); } else @@ -542,6 +546,7 @@ bool RsGxsDataAccess::getMsgSummary(const uint32_t& token, GxsMsgMetaResult& msg if(mmreq) { msgInfo = mmreq->mMsgMetaData; + mmreq->mMsgMetaData.clear(); locked_updateRequestStatus(token, GXS_REQUEST_V2_STATUS_DONE); } @@ -581,6 +586,7 @@ bool RsGxsDataAccess::getMsgRelatedSummary(const uint32_t &token, MsgRelatedMeta if(mrireq) { msgMeta = mrireq->mMsgMetaResult; + mrireq->mMsgMetaResult.clear(); locked_updateRequestStatus(token, GXS_REQUEST_V2_STATUS_DONE); } else @@ -619,6 +625,7 @@ bool RsGxsDataAccess::getMsgRelatedList(const uint32_t &token, MsgRelatedIdResul if(mrireq) { msgIds = mrireq->mMsgIdResult; + mrireq->mMsgIdResult.clear(); locked_updateRequestStatus(token, GXS_REQUEST_V2_STATUS_DONE); } else{ @@ -652,6 +659,7 @@ bool RsGxsDataAccess::getMsgList(const uint32_t& token, GxsMsgIdResult& msgIds) if(mireq) { msgIds = mireq->mMsgIdResult; + mireq->mMsgIdResult.clear(); locked_updateRequestStatus(token, GXS_REQUEST_V2_STATUS_DONE); } else{ @@ -684,6 +692,7 @@ bool RsGxsDataAccess::getGroupList(const uint32_t& token, std::listmGroupIdResult; + gireq->mGroupIdResult.clear(); locked_updateRequestStatus(token, GXS_REQUEST_V2_STATUS_DONE); }else{ diff --git a/libretroshare/src/gxs/rsgxsrequesttypes.cc b/libretroshare/src/gxs/rsgxsrequesttypes.cc new file mode 100644 index 000000000..877569c65 --- /dev/null +++ b/libretroshare/src/gxs/rsgxsrequesttypes.cc @@ -0,0 +1,61 @@ +/* + * libretroshare/src/gxs: rgxsrequesttypes.cc + * + * Type introspect request types for data access request implementation + * + * Copyright 2012-2012 by Christopher Evi-Parker + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License Version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + * Please report all bugs and problems to "retroshare@lunamutt.com". + * + */ + +#include "rsgxsrequesttypes.h" +#include "util/rsstd.h" + +GroupMetaReq::~GroupMetaReq() +{ + rsstd::delete_all(mGroupMetaData.begin(), mGroupMetaData.end()); +} + +GroupDataReq::~GroupDataReq() +{ + rsstd::delete_all(mGroupData.begin(), mGroupData.end()); +} + +MsgMetaReq::~MsgMetaReq() +{ + for (GxsMsgMetaResult::iterator it = mMsgMetaData.begin(); it != mMsgMetaData.end(); ++it) { + rsstd::delete_all(it->second.begin(), it->second.end()); + } +} + +MsgDataReq::~MsgDataReq() +{ + for (NxsMsgDataResult::iterator it = mMsgData.begin(); it != mMsgData.end(); ++it) { + rsstd::delete_all(it->second.begin(), it->second.end()); + } +} + +MsgRelatedInfoReq::~MsgRelatedInfoReq() +{ + for (MsgRelatedMetaResult::iterator metaIt = mMsgMetaResult.begin(); metaIt != mMsgMetaResult.end(); ++metaIt) { + rsstd::delete_all(metaIt->second.begin(), metaIt->second.end()); + } + for (NxsMsgRelatedDataResult::iterator dataIt = mMsgDataResult.begin(); dataIt != mMsgDataResult.end(); ++dataIt) { + rsstd::delete_all(dataIt->second.begin(), dataIt->second.end()); + } +} diff --git a/libretroshare/src/gxs/rsgxsrequesttypes.h b/libretroshare/src/gxs/rsgxsrequesttypes.h index 1c743fe61..a90928f0f 100644 --- a/libretroshare/src/gxs/rsgxsrequesttypes.h +++ b/libretroshare/src/gxs/rsgxsrequesttypes.h @@ -31,23 +31,24 @@ class GxsRequest { +public: + virtual ~GxsRequest() {} public: - - - virtual ~GxsRequest() { return; } uint32_t token; uint32_t reqTime; uint32_t ansType; uint32_t reqType; - RsTokReqOptions Options; + RsTokReqOptions Options; uint32_t status; }; class GroupMetaReq : public GxsRequest { +public: + virtual ~GroupMetaReq(); public: std::list mGroupIds; @@ -56,41 +57,42 @@ public: class GroupIdReq : public GxsRequest { - public: - std::list mGroupIds; std::list mGroupIdResult; }; class GroupDataReq : public GxsRequest { +public: + virtual ~GroupDataReq(); public: - std::list mGroupIds; + std::list mGroupIds; std::list mGroupData; }; - class MsgIdReq : public GxsRequest { - public: - GxsMsgReq mMsgIds; GxsMsgIdResult mMsgIdResult; }; class MsgMetaReq : public GxsRequest { +public: + virtual ~MsgMetaReq(); public: GxsMsgReq mMsgIds; - GxsMsgMetaResult mMsgMetaData; + GxsMsgMetaResult mMsgMetaData; }; class MsgDataReq : public GxsRequest { +public: + virtual ~MsgDataReq(); public: GxsMsgReq mMsgIds; @@ -100,30 +102,31 @@ public: class ServiceStatisticRequest: public GxsRequest { public: - GxsServiceStatistic mServiceStatistic; + GxsServiceStatistic mServiceStatistic; }; struct GroupStatisticRequest: public GxsRequest { public: - RsGxsGroupId mGrpId; - GxsGroupStatistic mGroupStatistic; + RsGxsGroupId mGrpId; + GxsGroupStatistic mGroupStatistic; }; class MsgRelatedInfoReq : public GxsRequest { +public: + virtual ~MsgRelatedInfoReq(); public: - std::vector mMsgIds; - MsgRelatedIdResult mMsgIdResult; - MsgRelatedMetaResult mMsgMetaResult; - NxsMsgRelatedDataResult mMsgDataResult; + std::vector mMsgIds; + MsgRelatedIdResult mMsgIdResult; + MsgRelatedMetaResult mMsgMetaResult; + NxsMsgRelatedDataResult mMsgDataResult; }; class GroupSetFlagReq : public GxsRequest { public: - const static uint32_t FLAG_SUBSCRIBE; const static uint32_t FLAG_STATUS; @@ -131,13 +134,11 @@ public: uint32_t flag; uint32_t flagMask; RsGxsGroupId grpId; - }; class MessageSetFlagReq : public GxsRequest { public: - const static uint32_t FLAG_STATUS; uint8_t type; @@ -146,5 +147,4 @@ public: RsGxsGrpMsgIdPair msgId; }; - #endif /* RSGXSREQUESTTYPES_H_ */ diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index 3eec6b908..e35a8742c 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -452,6 +452,7 @@ HEADERS += util/folderiterator.h \ util/dnsresolver.h \ util/rsprint.h \ util/rsstring.h \ + util/rsstd.h \ util/rsthreads.h \ util/rsversioninfo.h \ util/rswin.h \ @@ -680,7 +681,8 @@ gxs { gxs/rsgxsifacehelper.h \ gxs/gxstokenqueue.h \ gxs/rsgxsnetutils.h \ - gxs/rsgxsiface.h + gxs/rsgxsiface.h \ + gxs/rsgxsrequesttypes.h SOURCES += serialiser/rsnxsitems.cc \ @@ -696,7 +698,8 @@ gxs { gxs/gxssecurity.cc \ gxs/gxstokenqueue.cc \ gxs/rsgxsnetutils.cc \ - gxs/rsgxsutil.cc + gxs/rsgxsutil.cc \ + gxs/rsgxsrequesttypes.cc # Identity Service diff --git a/libretroshare/src/retroshare/rsgxsifacehelper.h b/libretroshare/src/retroshare/rsgxsifacehelper.h index eb8ea368d..376442e74 100644 --- a/libretroshare/src/retroshare/rsgxsifacehelper.h +++ b/libretroshare/src/retroshare/rsgxsifacehelper.h @@ -178,7 +178,7 @@ public: * @param msgInfo the message metadata returned for given request token * @return false if request token is invalid, check token status for error report */ - bool getMsgrelatedSummary(const uint32_t &token, GxsMsgRelatedMetaMap &msgInfo) + bool getMsgRelatedSummary(const uint32_t &token, GxsMsgRelatedMetaMap &msgInfo) { return mGxs->getMsgRelatedMeta(token, msgInfo); } diff --git a/libretroshare/src/util/rsstd.h b/libretroshare/src/util/rsstd.h new file mode 100644 index 000000000..b2ff78887 --- /dev/null +++ b/libretroshare/src/util/rsstd.h @@ -0,0 +1,37 @@ +/**************************************************************** + * This file is distributed under the following license: + * + * Copyright (c) 2015, RetroShare Team + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ****************************************************************/ + +#ifndef RSSTD_H_ +#define RSSTD_H_ + +namespace rsstd { + +template +void delete_all(_IIter iter_begin, _IIter iter_end) +{ + for (_IIter it = iter_begin; it != iter_end; ++it) { + delete(*it); + } +} + +} + +#endif // RSSTD_H_