Commit 7d2da0ca authored by Simon Morlat's avatar Simon Morlat

remove some memory leaks

use make_shared<>() instead of shared_ptr<T>(new T), because it allocates one memory block instead of two for the latter case.
parent ea9cb98d
......@@ -187,11 +187,13 @@ void RequestSipEvent::setIncomingAgent(const shared_ptr<IncomingAgent> &agent) {
LOGA("Can't change incoming agent in request sip event");
}
shared_ptr<IncomingTransaction> RequestSipEvent::createIncomingTransaction() {
shared_ptr<IncomingTransaction> transaction = dynamic_pointer_cast<IncomingTransaction>(mIncomingAgent);
if (transaction == NULL) {
if (!mMsgSip->mOriginal) LOGA("It is too late to create an incoming transaction");
transaction = shared_ptr<IncomingTransaction>(new IncomingTransaction(mIncomingAgent->getAgent()));
transaction = IncomingTransaction::create(mIncomingAgent->getAgent());
mIncomingAgent = transaction;
transaction->handle(mMsgSip);
......@@ -203,7 +205,7 @@ shared_ptr<IncomingTransaction> RequestSipEvent::createIncomingTransaction() {
shared_ptr<OutgoingTransaction> RequestSipEvent::createOutgoingTransaction() {
shared_ptr<OutgoingTransaction> transaction = dynamic_pointer_cast<OutgoingTransaction>(mOutgoingAgent);
if (transaction == NULL) {
transaction = shared_ptr<OutgoingTransaction>(new OutgoingTransaction(mOutgoingAgent->getAgent()));
transaction = OutgoingTransaction::create(mOutgoingAgent->getAgent());
mOutgoingAgent = transaction;
linkTransactions();
}
......
......@@ -46,6 +46,7 @@ class MsgSip {
friend class IncomingTransaction;
friend class OutgoingTransaction;
public:
MsgSip(msg_t *msg);
MsgSip(const MsgSip &msgSip);
MsgSip(const MsgSip &msgSip, msg_t *msg);
~MsgSip();
......@@ -68,9 +69,8 @@ public:
inline std::shared_ptr<SipAttributes> getSipAttr() { return mSipAttr; }
const char *print();
private:
MsgSip(msg_t *msg);
static inline std::shared_ptr<MsgSip> createFromOriginalMsg(msg_t *msg) {
return std::shared_ptr<MsgSip>(new MsgSip(msg));
return std::make_shared<MsgSip>(msg);
}
void defineMsg(msg_t *msg);
mutable su_home_t *mHome;
......
......@@ -45,7 +45,7 @@ static string subKey(const string &key, size_t *pos) {
}
inline static string cstring_get(const string &key, size_t pos, const char *str) {
if (!str) throw new invalid_argument("Null string found in sip msg for " + key);
if (!str) throw invalid_argument("Null string found in sip msg for " + key);
return str;
}
inline static string cstring_or_empty_get(const string &key, size_t pos, const char *str) {
......@@ -66,14 +66,14 @@ static string url_get(const string &key, size_t pos, const url_t *url) {
if (id == "domain") return cstring_get(key, pos, url->url_host);
if (id == "user") return cstring_get(key, pos, url->url_user);
if (id == "params") return cstring_or_empty_get(key, pos, url->url_params);
throw new runtime_error("url_get: unhandled arg '" + id + "' in " + key);
throw runtime_error("url_get: unhandled arg '" + id + "' in " + key);
}
static string addr_get(const string &key, size_t pos, const sip_addr_s *addr) {
string id=subKey(key, &pos);
if (!addr) throw new invalid_argument("No address found in sip msg for " + key);
if (id == "uri") return url_get(key, pos, addr->a_url);
throw new runtime_error("addr_get: unhandled arg '" + id + "' in " + key);
throw runtime_error("addr_get: unhandled arg '" + id + "' in " + key);
}
static string request_get(const string &key, size_t pos, const sip_request_t *req) {
......@@ -81,7 +81,7 @@ static string request_get(const string &key, size_t pos, const sip_request_t *re
if (!req) throw new invalid_argument("No request found in sip msg for " + key);
if (id == "uri") return url_get(key, pos, req->rq_url);
if (id == "mn" || id == "method-name") return cstring_get(key, pos, req->rq_method_name);
throw new runtime_error("request_get: unhandled arg '" + id + "' in " + key);
throw runtime_error("request_get: unhandled arg '" + id + "' in " + key);
}
static string ua_get(const string &key, size_t pos, const sip_user_agent_t *ua) {
......@@ -94,14 +94,14 @@ static string status_get(const string &key, size_t pos, const sip_status_s *stat
if (!status) throw new invalid_argument("No status found in sip msg for " + key);
if (id == "phrase") return cstring_get(key, pos, status->st_phrase);
if (id == "code") return int_get(key, pos, status->st_status);
throw new runtime_error("status_get: unhandled arg '" + id + "' in " + key);
throw runtime_error("status_get: unhandled arg '" + id + "' in " + key);
}
static string callid_get(const string &key, size_t pos, const sip_call_id_s *callid) {
string id=subKey(key, &pos);
if (id.empty()) return cstring_get(key, pos, callid->i_id);
if (id == "hash") return int_get(key, pos, callid->i_hash);
throw new runtime_error("callid_get: unhandled arg '" + id + "' in " + key);
throw runtime_error("callid_get: unhandled arg '" + id + "' in " + key);
}
static bool is_request(sip_t *sip) {
......@@ -121,7 +121,7 @@ std::string SipAttributes::get(const std::string &key) const {
if (id == "ua" || id == "user-agent") return ua_get(key, pos, sip->sip_user_agent);
if (id == "callid" ) return callid_get(key, pos, sip->sip_call_id);
throw new runtime_error("unhandled arg '" + id + "' in '" + key+ "'");
throw runtime_error("unhandled arg '" + id + "' in '" + key+ "'");
}
bool SipAttributes::isTrue(const string &key) const {
......@@ -130,5 +130,5 @@ bool SipAttributes::isTrue(const string &key) const {
} else if (key == "is_response") {
return !is_request(sip);
}
throw new runtime_error("unhandled true/false " + key);
throw runtime_error("unhandled true/false " + key);
};
......@@ -43,6 +43,10 @@ OutgoingTransaction::OutgoingTransaction(Agent *agent) :
LOGD("New OutgoingTransaction %p", this);
}
shared_ptr<OutgoingTransaction> OutgoingTransaction::create(Agent *agent){
return make_shared<OutgoingTransaction>(agent);
}
OutgoingTransaction::~OutgoingTransaction() {
LOGD("Delete OutgoingTransaction %p", this);
}
......@@ -112,8 +116,8 @@ int OutgoingTransaction::_callback(nta_outgoing_magic_t *magic, nta_outgoing_t *
if (sip != NULL) {
msg_t *msg = nta_outgoing_getresponse(otr->mOutgoing);
auto oagent=dynamic_pointer_cast<OutgoingAgent>(otr->shared_from_this());
auto msgsip=shared_ptr<MsgSip>(new MsgSip(msg));
shared_ptr<ResponseSipEvent> sipevent(new ResponseSipEvent(oagent, msgsip));
auto msgsip=make_shared<MsgSip>(msg);
shared_ptr<ResponseSipEvent> sipevent=make_shared<ResponseSipEvent>(oagent, msgsip);
msg_destroy(msg);
otr->mAgent->sendResponseEvent(sipevent);
......@@ -142,6 +146,10 @@ IncomingTransaction::IncomingTransaction(Agent *agent) :
LOGD("New IncomingTransaction %p", this);
}
shared_ptr<IncomingTransaction> IncomingTransaction::create(Agent *agent){
return make_shared<IncomingTransaction>(agent);
}
void IncomingTransaction::handle(const shared_ptr<MsgSip> &ms) {
msg_t* msg = ms->mOriginalMsg;
msg_ref_create(msg);
......@@ -162,7 +170,7 @@ IncomingTransaction::~IncomingTransaction() {
shared_ptr<MsgSip> IncomingTransaction::createResponse(int status, char const *phrase) {
msg_t *msg = nta_incoming_create_response(mIncoming, status, phrase);
shared_ptr<MsgSip> ms(shared_ptr<MsgSip>(new MsgSip(msg)));
shared_ptr<MsgSip> ms=make_shared<MsgSip>(msg);
msg_destroy(msg);
return ms;
}
......
......@@ -136,6 +136,8 @@ struct TransactionEvent {
class OutgoingTransaction: public Transaction, public OutgoingAgent, public std::enable_shared_from_this<OutgoingTransaction> {
public:
//the use of make_shared() requires the constructor to be public, but don't use it. Use RequestSipEvent::createOutgoingTransaction().
OutgoingTransaction(Agent *agent);
void cancel();
const url_t *getRequestUri()const;
const std::string &getBranchId()const;
......@@ -148,20 +150,19 @@ public:
std::shared_ptr<IncomingTransaction> mIncoming;
private:
friend class RequestSipEvent;
OutgoingTransaction(Agent *agent);
static std::shared_ptr<OutgoingTransaction> create(Agent *agent);
std::shared_ptr<OutgoingTransaction> mSofiaRef;
nta_outgoing_t *mOutgoing;
std::string mBranchId;
virtual void send(const std::shared_ptr<MsgSip> &msg, url_string_t const *u, tag_type_t tag, tag_value_t value, ...);
void destroy();
private:
static int _callback(nta_outgoing_magic_t *magic, nta_outgoing_t *irq, const sip_t *sip);
};
class IncomingTransaction: public Transaction, public IncomingAgent, public std::enable_shared_from_this<IncomingTransaction> {
public:
//the use of make_shared() requires the constructor to be public, but don't use it. Use RequestSipEvent::createIncomingTransaction().
IncomingTransaction(Agent *agent);
void handle(const std::shared_ptr<MsgSip> &ms);
std::shared_ptr<MsgSip> createResponse(int status, char const *phrase);
~IncomingTransaction();
......@@ -172,7 +173,8 @@ public:
std::shared_ptr<OutgoingTransaction> mOutgoing;
private:
friend class RequestSipEvent;
IncomingTransaction(Agent *agent);
static std::shared_ptr<IncomingTransaction> create(Agent *agent);
std::shared_ptr<IncomingTransaction> mSofiaRef;
nta_incoming_t *mIncoming;
......@@ -180,8 +182,6 @@ private:
virtual void reply(const std::shared_ptr<MsgSip> &msg, int status, char const *phrase, tag_type_t tag, tag_value_t value, ...);
void destroy();
private:
static int _callback(nta_incoming_magic_t *magic, nta_incoming_t *irq, const sip_t *sip);
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment