From d8864d3b7c5dd8d6b0ed1c0767c645108cb9f74e Mon Sep 17 00:00:00 2001
From: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
Date: Wed, 10 Aug 2011 10:45:43 -0400
Subject: [PATCH] * #6596: create local SDP on the stack, not the heap

---
 .../audio/audiortp/AudioSymmetricRtpSession.cpp  |  4 ++++
 sflphone-common/src/sip/sdp.cpp                  |  1 +
 sflphone-common/src/sip/sipcall.cpp              | 16 +++-------------
 sflphone-common/src/sip/sipcall.h                | 14 ++++++--------
 4 files changed, 14 insertions(+), 21 deletions(-)

diff --git a/sflphone-common/src/audio/audiortp/AudioSymmetricRtpSession.cpp b/sflphone-common/src/audio/audiortp/AudioSymmetricRtpSession.cpp
index 669403ccc6..0f9905f0d2 100644
--- a/sflphone-common/src/audio/audiortp/AudioSymmetricRtpSession.cpp
+++ b/sflphone-common/src/audio/audiortp/AudioSymmetricRtpSession.cpp
@@ -54,12 +54,16 @@ AudioSymmetricRtpSession::AudioSymmetricRtpSession (SIPCall * sipcall) :
 
 AudioSymmetricRtpSession::~AudioSymmetricRtpSession()
 {
+    // XXX: DON'T call any members of this (i.e. AudioSymmetricRtpSession and
+    // the classes from which it is derived, or touch any of their data
     _info ("AudioSymmetricRtpSession: Delete AudioSymmetricRtpSession instance");
 }
 
 void AudioSymmetricRtpSession::final()
 {
     delete _rtpThread;
+
+    // See: http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.15
     delete this;
 }
 
diff --git a/sflphone-common/src/sip/sdp.cpp b/sflphone-common/src/sip/sdp.cpp
index 8c4b23fba6..91e44a92d2 100644
--- a/sflphone-common/src/sip/sdp.cpp
+++ b/sflphone-common/src/sip/sdp.cpp
@@ -648,6 +648,7 @@ Sdp::~Sdp()
 
     for (iter = localAudioMediaCap_.begin(); iter != localAudioMediaCap_.end(); ++iter)
         delete *iter;
+    pj_pool_release (memPool_);
 }
 
 
diff --git a/sflphone-common/src/sip/sipcall.cpp b/sflphone-common/src/sip/sipcall.cpp
index 6dd14092de..b577d77c6e 100644
--- a/sflphone-common/src/sip/sipcall.cpp
+++ b/sflphone-common/src/sip/sipcall.cpp
@@ -46,28 +46,18 @@ SIPCall::SIPCall (const std::string& id, Call::CallType type, pj_caching_pool *c
     , _audiortp (new sfl::AudioRtpFactory(this))
     , _xferSub (NULL)
     , _invSession (NULL)
-    , _local_sdp (NULL)
-	, pool_(NULL)
+	, pool_(pj_pool_create(&caching_pool->factory, id.c_str(), CALL_MEMPOOL_INIT_SIZE,
+                            CALL_MEMPOOL_INC_SIZE, NULL))
+    , local_sdp_(pool_)
 {
     _debug ("SIPCall: Create new call %s", id.c_str());
-
-    // Create memory pool for application, initialization value is based on empiric values.
-    pool_ = pj_pool_create (&caching_pool->factory, id.c_str(), CALL_MEMPOOL_INIT_SIZE,
-                            CALL_MEMPOOL_INC_SIZE, NULL);
-
-    _local_sdp = new Sdp (pool_);
 }
 
 SIPCall::~SIPCall()
 {
     _debug ("SIPCall: Delete call");
-
     _debug ("SDP: pool capacity %d", pj_pool_get_capacity (pool_));
     _debug ("SDP: pool size %d", pj_pool_get_used_size (pool_));
 
-    pj_pool_release (pool_);
-    pool_ = 0;
-    // Release memory allocated for SDP
-    delete _local_sdp;
     delete _audiortp;
 }
diff --git a/sflphone-common/src/sip/sipcall.h b/sflphone-common/src/sip/sipcall.h
index 1d9366f95b..083cc6f386 100644
--- a/sflphone-common/src/sip/sipcall.h
+++ b/sflphone-common/src/sip/sipcall.h
@@ -33,9 +33,9 @@
 #define SIPCALL_H
 
 #include "call.h"
+#include "sdp.h"
 #include <cassert>
 
-class Sdp;
 class pjsip_evsub;
 class pj_caching_pool;
 class pj_pool_t;
@@ -96,7 +96,7 @@ class SIPCall : public Call
          * Return the local SDP session
          */
         Sdp* getLocalSDP (void) {
-            return _local_sdp;
+            return &local_sdp_;
         }
 
         /**
@@ -110,7 +110,6 @@ class SIPCall : public Call
          * Return the local memory pool for this call
          */
         pj_pool_t *getMemoryPool(void) {
-            assert(pool_);
             return pool_;
         }
 
@@ -162,16 +161,15 @@ class SIPCall : public Call
          */
         pjsip_inv_session *_invSession;
 
-        /**
-         * The SDP session
-         */
-        Sdp *_local_sdp;
-
         /**
          * The pool to allocate memory, released once call hang up
          */
         pj_pool_t *pool_;
 
+        /**
+         * The SDP session
+         */
+        Sdp local_sdp_;
 };
 
 #endif
-- 
GitLab