Changing mailbox parameter from native pointer to smartpointer

The SmartPointer is needed as due to multi threading it is not know
who should delete the mailbox at the end. Either the generated code needs
to delete it or the stack itself. This problem is solved by using
smartpointers.

Change-Id: I2c1ac6ca3ed126afa438e700f5849ffdaf03b2bb

git-svn-id: https://svn.apache.org/repos/asf/etch/trunk@1578873 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/binding-cpp/compiler/src/main/resources/org/apache/etch/bindings/cpp/compiler/remote_cpp.vm b/binding-cpp/compiler/src/main/resources/org/apache/etch/bindings/cpp/compiler/remote_cpp.vm
index f611163..b242562 100644
--- a/binding-cpp/compiler/src/main/resources/org/apache/etch/bindings/cpp/compiler/remote_cpp.vm
+++ b/binding-cpp/compiler/src/main/resources/org/apache/etch/bindings/cpp/compiler/remote_cpp.vm
@@ -35,7 +35,7 @@
 #foreach($n in $intf.iterator())
 #if($n.isMsgDir($mc))
 #if(!$n.isHidden())
-$clname::$n.name()AsyncResultRemote::$n.name()AsyncResultRemote($clname* base, EtchMailbox* mailbox) 
+$clname::$n.name()AsyncResultRemote::$n.name()AsyncResultRemote($clname* base, capu::SmartPointer<EtchMailbox> mailbox)
   : #if($n.hasReturn())EtchAsyncResult<$helper.getEtchTypeName($n.type(), false)>(base->mRuntime, mailbox)#{else}EtchAsyncResultNone(base->mRuntime, mailbox)#end {
   mBase = base;
 }
@@ -101,7 +101,7 @@
   msg->put($vfname::$p.vname($helper)(), obj$ObjCount);
 #end
 
-  EtchMailbox* mb = NULL; 
+  capu::SmartPointer<EtchMailbox> mb = NULL;
   status = base->begincall(msg, mb);
   $n.name()AsyncResultRemote* result = new $n.name()AsyncResultRemote(base, mb);
 
diff --git a/binding-cpp/compiler/src/main/resources/org/apache/etch/bindings/cpp/compiler/remote_h.vm b/binding-cpp/compiler/src/main/resources/org/apache/etch/bindings/cpp/compiler/remote_h.vm
index 2097c78..1593392 100644
--- a/binding-cpp/compiler/src/main/resources/org/apache/etch/bindings/cpp/compiler/remote_h.vm
+++ b/binding-cpp/compiler/src/main/resources/org/apache/etch/bindings/cpp/compiler/remote_h.vm
@@ -64,7 +64,7 @@
       /**
        * Create a new instance from $clname class
        */
-      $n.name()AsyncResultRemote($clname* base, EtchMailbox* mailbox);
+      $n.name()AsyncResultRemote($clname* base, capu::SmartPointer<EtchMailbox> mailbox);
 
       /**
        * Destructor
diff --git a/binding-cpp/runtime/include/support/EtchAsyncResult.h b/binding-cpp/runtime/include/support/EtchAsyncResult.h
index 4940b4c..dfd5f83 100644
--- a/binding-cpp/runtime/include/support/EtchAsyncResult.h
+++ b/binding-cpp/runtime/include/support/EtchAsyncResult.h
@@ -34,7 +34,7 @@
    * @param runtime
    * @param mailbox
    */
-  EtchAsyncResult(EtchRuntime* runtime = NULL, EtchMailbox* mailbox = NULL)
+  EtchAsyncResult(EtchRuntime* runtime = NULL, capu::SmartPointer<EtchMailbox> mailbox = NULL)
    : EtchAsyncResultNone(runtime, mailbox), mResult(NULL), mHasResult(false) {
   }
 
diff --git a/binding-cpp/runtime/include/support/EtchAsyncResultNone.h b/binding-cpp/runtime/include/support/EtchAsyncResultNone.h
index ea5d5f6..111dfb4 100644
--- a/binding-cpp/runtime/include/support/EtchAsyncResultNone.h
+++ b/binding-cpp/runtime/include/support/EtchAsyncResultNone.h
@@ -36,7 +36,7 @@
    * Constructor
    * @param runtime
    */
-  explicit EtchAsyncResultNone(EtchRuntime* runtime = NULL, EtchMailbox* mailbox = NULL);
+  explicit EtchAsyncResultNone(EtchRuntime* runtime = NULL, capu::SmartPointer<EtchMailbox> mailbox = NULL);
 
   /**
    * Destructor
@@ -71,7 +71,7 @@
   void setMailboxStatus();
 
   EtchRuntime* mRuntime;
-  EtchMailbox* mMailbox;
+  capu::SmartPointer<EtchMailbox> mMailbox;
   capu::Mutex mMutex;
   capu::CondVar mCond;
   capu::bool_t mHasMailboxStatus;
diff --git a/binding-cpp/runtime/include/support/EtchDeliveryService.h b/binding-cpp/runtime/include/support/EtchDeliveryService.h
index c874ab9..a82bd27 100644
--- a/binding-cpp/runtime/include/support/EtchDeliveryService.h
+++ b/binding-cpp/runtime/include/support/EtchDeliveryService.h
@@ -38,7 +38,7 @@
    * @return ETCH_OK if the message was send.

    *         ETCH_ERROR if there is a problem sending.

    */

-  virtual status_t begincall(capu::SmartPointer<EtchMessage> msg, EtchMailbox*& result) = 0;
+  virtual status_t begincall(capu::SmartPointer<EtchMessage> msg, capu::SmartPointer<EtchMailbox>& result) = 0;
 

   /**

    * Finishes a call sequence by waiting for the response message.

diff --git a/binding-cpp/runtime/include/support/EtchRemoteBase.h b/binding-cpp/runtime/include/support/EtchRemoteBase.h
index d41a499..d6dea55 100644
--- a/binding-cpp/runtime/include/support/EtchRemoteBase.h
+++ b/binding-cpp/runtime/include/support/EtchRemoteBase.h
@@ -71,7 +71,7 @@
    * @return a mailbox which can be used to read the response, using
    * {@link #endcall(Mailbox, Type)}.
    */
-  status_t begincall(capu::SmartPointer<EtchMessage> msg, EtchMailbox *&result);
+  status_t begincall(capu::SmartPointer<EtchMessage> msg, capu::SmartPointer<EtchMailbox> &result);
 
   /**
    * Finishes a call sequence by waiting for the response message.
diff --git a/binding-cpp/runtime/include/transport/EtchDefaultDeliveryService.h b/binding-cpp/runtime/include/transport/EtchDefaultDeliveryService.h
index 1583f79..65a320c 100644
--- a/binding-cpp/runtime/include/transport/EtchDefaultDeliveryService.h
+++ b/binding-cpp/runtime/include/transport/EtchDefaultDeliveryService.h
@@ -122,7 +122,7 @@
   /**
    * @see EtchDeliveryService
    */
-  virtual status_t begincall(capu::SmartPointer<EtchMessage> msg, EtchMailbox*& result);
+  virtual status_t begincall(capu::SmartPointer<EtchMessage> msg, capu::SmartPointer<EtchMailbox>& result);
 
   /**
    * @see EtchDeliveryService
diff --git a/binding-cpp/runtime/include/transport/EtchMailboxManager.h b/binding-cpp/runtime/include/transport/EtchMailboxManager.h
index 75b2556..0aaa16b 100644
--- a/binding-cpp/runtime/include/transport/EtchMailboxManager.h
+++ b/binding-cpp/runtime/include/transport/EtchMailboxManager.h
@@ -40,7 +40,7 @@
    * @param result the mailbox which will receive responses to this call.
    * @return status code
    */
-  virtual status_t transportCall(capu::SmartPointer<EtchWho> recipient, capu::SmartPointer<EtchMessage> msg, EtchMailbox*& result) = 0;
+  virtual status_t transportCall(capu::SmartPointer<EtchWho> recipient, capu::SmartPointer<EtchMessage> msg, capu::SmartPointer<EtchMailbox>& result) = 0;
 
   /**
    * Removes the mailbox from the set of mailboxes receiving responses to
@@ -48,7 +48,7 @@
    * @param mb a mailbox as returned by {@link #transportCall(Who, Message)}.
    * @return status code
    */
-  virtual status_t unregisterMailbox(EtchMailbox* mb) = 0;
+  virtual status_t unregisterMailbox(EtchLong mailboxId) = 0;
 
   /**
    * Re-delivers dead letter messages from a closed mailbox.
diff --git a/binding-cpp/runtime/include/transport/EtchPlainMailboxManager.h b/binding-cpp/runtime/include/transport/EtchPlainMailboxManager.h
index 4dc83dd..074a4a3 100644
--- a/binding-cpp/runtime/include/transport/EtchPlainMailboxManager.h
+++ b/binding-cpp/runtime/include/transport/EtchPlainMailboxManager.h
@@ -59,19 +59,19 @@
    * to messages.
    * @param mb
    */
-  status_t registerMailbox(EtchMailbox* mb);
+  status_t registerMailbox(capu::SmartPointer<EtchMailbox> mb);
 
   /**
    * @see EtchMailboxManager
    */
-  status_t unregisterMailbox(EtchMailbox* mb);
+  status_t unregisterMailbox(EtchLong mailboxId);
 
   /**
    * Returns the mailbox for the specified msgid. This is a testing api.
    * @param msgid
    * @return the mailbox for the specified msgid.
    */
-  status_t getMailbox(EtchLong msgid, EtchMailbox*& result);
+  status_t getMailbox(EtchLong msgid, capu::SmartPointer<EtchMailbox>& result);
 
   ////////////////////////////
   // MessageHandler methods //
@@ -81,7 +81,7 @@
   ///////////////////////////
   // MessageSource methods //
   ///////////////////////////
-  status_t transportCall(capu::SmartPointer<EtchWho> recipient, capu::SmartPointer<EtchMessage> msg, EtchMailbox*& result);
+  status_t transportCall(capu::SmartPointer<EtchWho> recipient, capu::SmartPointer<EtchMessage> msg, capu::SmartPointer<EtchMailbox>& result);
   status_t transportMessage(capu::SmartPointer<EtchWho> recipient, capu::SmartPointer<EtchMessage> message);
 
   ///////////////////////////
@@ -109,7 +109,7 @@
   EtchSessionMessage* mSession;
   EtchTransportMessage* mTransport;
   capu::bool_t mUp;
-  EtchHashTable<EtchLong, EtchMailbox*> mMailboxes;
+  EtchHashTable<EtchLong, capu::SmartPointer<EtchMailbox> > mMailboxes;
   capu::Mutex mMutex;
   EtchIdGenerator mIdGen;
 };
diff --git a/binding-cpp/runtime/src/main/support/EtchAsyncResultNone.cpp b/binding-cpp/runtime/src/main/support/EtchAsyncResultNone.cpp
index f8af524..e5597c4 100644
--- a/binding-cpp/runtime/src/main/support/EtchAsyncResultNone.cpp
+++ b/binding-cpp/runtime/src/main/support/EtchAsyncResultNone.cpp
@@ -20,25 +20,24 @@
 
 #define MAILBOX_NOTIFY_TIMEOUT 2000
 
-EtchAsyncResultNone::EtchAsyncResultNone(EtchRuntime* runtime, EtchMailbox* mailbox)
+EtchAsyncResultNone::EtchAsyncResultNone(EtchRuntime* runtime, capu::SmartPointer<EtchMailbox> mailbox)
   : mRuntime(runtime), mMailbox(mailbox), mHasMailboxStatus(false), mHasException(false), mException(NULL) {
-    if(mailbox != NULL) {
+    if(mailbox.get() != NULL) {
       mMailbox->registerNotify(this, NULL, MAILBOX_NOTIFY_TIMEOUT);
     }
     
 }
 
 EtchAsyncResultNone::~EtchAsyncResultNone() {
-  if(mMailbox != NULL) {
+  if(mMailbox.get() != NULL) {
     mMailbox->unregisterNotify(this);
     mMailbox->closeDelivery(false);
-    delete mMailbox;
   }
 }
 
 capu::bool_t EtchAsyncResultNone::hasException() {
   mMutex.lock();
-  while(mMailbox != NULL && !mHasMailboxStatus) {
+  while(mMailbox.get() != NULL && !mHasMailboxStatus) {
     mCond.wait(&mMutex);
   }
   mMutex.unlock();
diff --git a/binding-cpp/runtime/src/main/support/EtchPlainMailbox.cpp b/binding-cpp/runtime/src/main/support/EtchPlainMailbox.cpp
index d49957e..5024733 100644
--- a/binding-cpp/runtime/src/main/support/EtchPlainMailbox.cpp
+++ b/binding-cpp/runtime/src/main/support/EtchPlainMailbox.cpp
@@ -67,15 +67,16 @@
   s = mState;
   c = mQueue.isClosed();
   
-  mMutex.unlock();
-
   if (n != NULL) {
     n->mailboxStatus(this, s, c);
   }
+  mMutex.unlock();
 }
 
 status_t EtchPlainMailbox::read(EtchMailbox::EtchElement*& result) {
+  mMutex.lock();
   status_t status = mQueue.get(&result);
+  mMutex.unlock();
   if(ETCH_OK == status) {
     return ETCH_OK;
   }
@@ -84,7 +85,9 @@
 }
 
 status_t EtchPlainMailbox::read(EtchMailbox::EtchElement *& result, capu::int32_t maxDelay) {
+  mMutex.lock();
   status_t status = mQueue.get(&result, maxDelay);
+  mMutex.unlock();
   if(status == ETCH_OK) {
     return ETCH_OK;
   }
@@ -99,7 +102,7 @@
     return ETCH_EINVAL;
   }
 
-  mMailboxManager->unregisterMailbox(this);
+  mMailboxManager->unregisterMailbox(getMessageId());
   mQueue.close();
   mMutex.unlock();
 
diff --git a/binding-cpp/runtime/src/main/support/EtchRemoteBase.cpp b/binding-cpp/runtime/src/main/support/EtchRemoteBase.cpp
index ea39c24..a874a74 100644
--- a/binding-cpp/runtime/src/main/support/EtchRemoteBase.cpp
+++ b/binding-cpp/runtime/src/main/support/EtchRemoteBase.cpp
@@ -46,7 +46,7 @@
   return mSvc->transportMessage(NULL, msg);
 }
 
-status_t EtchRemoteBase::begincall(capu::SmartPointer<EtchMessage> msg, EtchMailbox *&result) {
+status_t EtchRemoteBase::begincall(capu::SmartPointer<EtchMessage> msg, capu::SmartPointer<EtchMailbox> &result) {
   ETCH_LOG_DEBUG(mRuntime->getLogger(), mRuntime->getLogger().getDeliveryServiceContext(), "Begin call for the message is initiated");
   return mSvc->begincall(msg, result);
 }
diff --git a/binding-cpp/runtime/src/main/transport/EtchDefaultDeliveryService.cpp b/binding-cpp/runtime/src/main/transport/EtchDefaultDeliveryService.cpp
index d1a0d14..aa0e2b5 100644
--- a/binding-cpp/runtime/src/main/transport/EtchDefaultDeliveryService.cpp
+++ b/binding-cpp/runtime/src/main/transport/EtchDefaultDeliveryService.cpp
@@ -141,7 +141,7 @@
   return mTransport->transportNotify(event);
 }
 
-status_t EtchDefaultDeliveryService::begincall(capu::SmartPointer<EtchMessage> msg, EtchMailbox*& result) {
+status_t EtchDefaultDeliveryService::begincall(capu::SmartPointer<EtchMessage> msg, capu::SmartPointer<EtchMailbox> &result) {
   ETCH_LOG_DEBUG(mRuntime->getLogger(), mRuntime->getLogger().getDeliveryServiceContext(), "Begin call for the message has been initiated");
   return mTransport->transportCall(NULL, msg, result);
 }
diff --git a/binding-cpp/runtime/src/main/transport/EtchPlainMailboxManager.cpp b/binding-cpp/runtime/src/main/transport/EtchPlainMailboxManager.cpp
index 922751b..1d670f6 100644
--- a/binding-cpp/runtime/src/main/transport/EtchPlainMailboxManager.cpp
+++ b/binding-cpp/runtime/src/main/transport/EtchPlainMailboxManager.cpp
@@ -28,13 +28,12 @@
 EtchPlainMailboxManager::~EtchPlainMailboxManager() {
   mMutex.lock();
 
-  EtchHashTable<EtchLong, EtchMailbox*>::Iterator it = mMailboxes.begin();
-  EtchHashTable<EtchLong, EtchMailbox*>::HashTableEntry entry;
+  EtchHashTable<EtchLong, capu::SmartPointer<EtchMailbox> >::Iterator it = mMailboxes.begin();
+  EtchHashTable<EtchLong, capu::SmartPointer<EtchMailbox> >::HashTableEntry entry;
 
   while (it.hasNext()) {
     it.next(&entry);
     entry.value->closeDelivery();
-    delete entry.value;
   }
 
   mMutex.unlock();
@@ -48,8 +47,8 @@
   return mSession->sessionMessage(sender, msg);
 }
 
-status_t EtchPlainMailboxManager::registerMailbox(EtchMailbox* mb) {
-  if(mb == NULL) {
+status_t EtchPlainMailboxManager::registerMailbox(capu::SmartPointer<EtchMailbox> mb) {
+  if(mb.get() == NULL) {
     return ETCH_EINVAL;
   }
 
@@ -59,7 +58,7 @@
     return ETCH_EINVAL;
   }
 
-  EtchMailbox* tmp = NULL;
+  capu::SmartPointer<EtchMailbox> tmp = NULL;
   if (mMailboxes.get(msgid, &tmp) != ETCH_ENOT_EXIST) {
     return ETCH_EINVAL;
   }
@@ -69,38 +68,42 @@
   return ETCH_OK;
 }
 
-status_t EtchPlainMailboxManager::unregisterMailbox(EtchMailbox* mb) {
-  if(mb == NULL) {
-    return ETCH_EINVAL;
-  }
-
-  EtchMailbox* tmp = NULL;
-  mMailboxes.remove(mb->getMessageId(), &tmp);
+status_t EtchPlainMailboxManager::unregisterMailbox(EtchLong mailboxId) {
+  capu::SmartPointer<EtchMailbox> tmp = NULL;
+  mMutex.lock();
+  mMailboxes.remove(mailboxId, &tmp);
+  mMutex.unlock();
   return ETCH_OK;
 }
 
-status_t EtchPlainMailboxManager::getMailbox(EtchLong msgid, EtchMailbox*& result) {
-  return mMailboxes.get(msgid, &result);
+status_t EtchPlainMailboxManager::getMailbox(EtchLong msgid, capu::SmartPointer<EtchMailbox>& result) {
+  status_t status = mMailboxes.get(msgid, &result);
+  return status;
 }
 
 status_t EtchPlainMailboxManager::sessionMessage(capu::SmartPointer<EtchWho> sender, capu::SmartPointer<EtchMessage> msg) {
   capu::int64_t msgid;
   if(msg->getInReplyToMessageId(msgid) == ETCH_OK) {
-    EtchMailbox* mb = NULL;
+    capu::SmartPointer<EtchMailbox> mb = NULL;
     ETCH_LOG_DEBUG(mRuntime->getLogger(), mRuntime->getLogger().getMailboxContext(), "A message has been received as answer to message identified by msgid " << msgid);
+	mMutex.lock();
     if (getMailbox(msgid, mb) != ETCH_OK) {
-      ETCH_LOG_ERROR(mRuntime->getLogger(), mRuntime->getLogger().getMailboxContext(), "Unable to get Mailbox fro msgid " << msgid);
+	  mMutex.unlock();
+      ETCH_LOG_ERROR(mRuntime->getLogger(), mRuntime->getLogger().getMailboxContext(), "Mailbox for Message with msgid " << msgid << "has already been closed and removed. Dropping message.");
       return ETCH_ERROR;
     }
+    mMutex.unlock();
     ETCH_LOG_DEBUG(mRuntime->getLogger(), mRuntime->getLogger().getMailboxContext(), "Message has been sent to respective mailbox");
-    return mb->message(sender, msg);
+	status_t status = mb->message(sender, msg);
+	
+    return status;
   }
   // no msgid - pass off to session
   ETCH_LOG_DEBUG(mRuntime->getLogger(), mRuntime->getLogger().getMailboxContext(), "Message has been sent to upper layer directly as no msgid was given");
   return mSession->sessionMessage(sender, msg);
 }
 
-status_t EtchPlainMailboxManager::transportCall(capu::SmartPointer<EtchWho> recipient, capu::SmartPointer<EtchMessage> msg, EtchMailbox*& result) {
+status_t EtchPlainMailboxManager::transportCall(capu::SmartPointer<EtchWho> recipient, capu::SmartPointer<EtchMessage> msg, capu::SmartPointer<EtchMailbox>& result) {
   capu::int64_t tmp;
   if (msg->getMessageId(tmp) == ETCH_OK) {
     // message has already been sent
@@ -118,10 +121,9 @@
   }
 
   ETCH_LOG_DEBUG(mRuntime->getLogger(), mRuntime->getLogger().getMailboxContext(), "A mailbox has been created for msgid " << msgid);
-  EtchMailbox *mb = new EtchPlainMailbox(this, msgid);
+  capu::SmartPointer<EtchMailbox> mb = new EtchPlainMailbox(this, msgid);
   mMutex.lock();
   if (registerMailbox(mb) != ETCH_OK) {
-    delete mb;
     ETCH_LOG_ERROR(mRuntime->getLogger(), mRuntime->getLogger().getMailboxContext(), "Mailbox registration failed");
     mMutex.unlock();
     return ETCH_ERROR;
@@ -134,7 +136,6 @@
     return ETCH_OK;
   } else {
     mb->closeDelivery();
-    delete mb;
     mMutex.unlock();
     return ETCH_ERROR;
   }
@@ -183,12 +184,11 @@
     ETCH_LOG_TRACE(mRuntime->getLogger(), mRuntime->getLogger().getMailboxContext(), "Connection is up");
   } else if (event->equals(&EtchSession::DOWN())) {
     mUp = false;
-    EtchHashTable<EtchLong, EtchMailbox*>::Iterator it = mMailboxes.begin();
-    EtchHashTable<EtchLong, EtchMailbox*>::HashTableEntry entry;
+    EtchHashTable<EtchLong, capu::SmartPointer<EtchMailbox> >::Iterator it = mMailboxes.begin();
+    EtchHashTable<EtchLong, capu::SmartPointer<EtchMailbox> >::HashTableEntry entry;
     while (it.hasNext()) {
       it.next(&entry);
       entry.value->closeDelivery();
-      delete entry.value;
     }
     ETCH_LOG_TRACE(mRuntime->getLogger(), mRuntime->getLogger().getMailboxContext(), "Connection is down");
   }
diff --git a/binding-cpp/runtime/src/test/support/EtchPlainMailboxTest.cpp b/binding-cpp/runtime/src/test/support/EtchPlainMailboxTest.cpp
index ef4ac22..fe80e57 100644
--- a/binding-cpp/runtime/src/test/support/EtchPlainMailboxTest.cpp
+++ b/binding-cpp/runtime/src/test/support/EtchPlainMailboxTest.cpp
@@ -30,7 +30,7 @@
   capu::bool_t unregistered;
   EtchList<EtchMailbox::EtchElement *> list;
 
-  virtual status_t unregisterMailbox(EtchMailbox* mb) {
+  virtual status_t unregisterMailbox(EtchLong mailboxId) {
     unregistered = true;
     return ETCH_OK;
   }
@@ -39,11 +39,11 @@
     return list.add(new EtchMailbox::EtchElement(sender, msg));
   }
 
-  MOCK_METHOD2(getMailbox, status_t(EtchLong msgid, EtchMailbox*& result));
+  MOCK_METHOD2(getMailbox, status_t(EtchLong msgid, capu::SmartPointer<EtchMailbox>& result));
 
   MOCK_METHOD2(sessionMessage, status_t(capu::SmartPointer<EtchWho> sender, capu::SmartPointer<EtchMessage> msg));
 
-  MOCK_METHOD3(transportCall, status_t(capu::SmartPointer<EtchWho> recipient, capu::SmartPointer<EtchMessage> msg, EtchMailbox*& result));
+  MOCK_METHOD3(transportCall, status_t(capu::SmartPointer<EtchWho> recipient, capu::SmartPointer<EtchMessage> msg, capu::SmartPointer<EtchMailbox>& result));
 
   MOCK_METHOD2(transportMessage, status_t(capu::SmartPointer<EtchWho> recipient, capu::SmartPointer<EtchMessage> message));
 
@@ -75,11 +75,10 @@
 
 TEST(EtchPlainMessageBoxTest, constructorTest) {
   MockMailboxManager2 manager;
-  EtchMailbox* mailbox = NULL;
+  capu::SmartPointer<EtchMailbox> mailbox = NULL;
   EtchLong id(5);
   mailbox = new EtchPlainMailbox(&manager, id);
-  EXPECT_TRUE(mailbox != NULL);
-  delete mailbox;
+  EXPECT_TRUE(mailbox.get() != NULL);
 }
 
 TEST(EtchPlainMessageBoxTest, closeDeliveryTest) {
diff --git a/binding-cpp/runtime/src/test/support/EtchRemoteBaseTest.cpp b/binding-cpp/runtime/src/test/support/EtchRemoteBaseTest.cpp
index 0c4e435..b7790b7 100644
--- a/binding-cpp/runtime/src/test/support/EtchRemoteBaseTest.cpp
+++ b/binding-cpp/runtime/src/test/support/EtchRemoteBaseTest.cpp
@@ -260,9 +260,9 @@
 
   manager->sessionNotify(new EtchString(EtchSession::UP()));
 
-  EtchMailbox *mail = NULL;
+  capu::SmartPointer<EtchMailbox> mail = NULL;
   EXPECT_TRUE(remote->begincall(msg, mail) == ETCH_OK);
-  EXPECT_TRUE(mail != NULL);
+  EXPECT_TRUE(mail.get() != NULL);
 
   factory->types.clear();
 
@@ -314,10 +314,10 @@
 
   manager->sessionNotify(new EtchString(EtchSession::UP()));
 
-  EtchMailbox *mail = NULL;
+  capu::SmartPointer<EtchMailbox> mail = NULL;
   EXPECT_TRUE(remote->begincall(message, mail) == ETCH_OK);
 
-  EXPECT_TRUE(mail != NULL);
+  EXPECT_TRUE(mail.get() != NULL);
 
   capu::int64_t id;
   message->getMessageId(id);
@@ -331,14 +331,13 @@
   //call the sessionMessage of mailbox manager as if it is called from messagizer to deliver data from
   EXPECT_TRUE(ETCH_OK == manager->sessionMessage(NULL, replymess));
   capu::SmartPointer<EtchObject> result;
-  EXPECT_TRUE(remote->endcall(mail, replyType, result) == ETCH_OK);
-  EXPECT_TRUE(mail != NULL);
+  EXPECT_TRUE(remote->endcall(mail.get(), replyType, result) == ETCH_OK);
+  EXPECT_TRUE(mail.get() != NULL);
   EXPECT_TRUE(result == data);
 
   factory->types.clear();
 
   delete transport;
-  delete mail;
   delete remote;
   delete manager;
   delete service;
diff --git a/binding-cpp/runtime/src/test/transport/EtchDefaultDeliveryServiceTest.cpp b/binding-cpp/runtime/src/test/transport/EtchDefaultDeliveryServiceTest.cpp
index 9e1fbc5..183aa10 100644
--- a/binding-cpp/runtime/src/test/transport/EtchDefaultDeliveryServiceTest.cpp
+++ b/binding-cpp/runtime/src/test/transport/EtchDefaultDeliveryServiceTest.cpp
@@ -175,7 +175,7 @@
   mailboxManager->sessionNotify(new EtchString(EtchSession::UP()));
 
   //test begincall
-  EtchMailbox *mail = NULL;
+  capu::SmartPointer<EtchMailbox> mail = NULL;
   EXPECT_TRUE(ETCH_OK == deliveryService->begincall(message, mail));
   EXPECT_EQ(1u, mailboxManager->count());
   EXPECT_TRUE(ETCH_OK == message->getMessageId(id));
@@ -233,7 +233,7 @@
   mailboxManager->sessionNotify(new EtchString(EtchSession::UP()));
 
   //performed the call
-  EtchMailbox *mail;
+  capu::SmartPointer<EtchMailbox> mail;
   EXPECT_TRUE(ETCH_OK == deliveryService->begincall(message, mail));
   EXPECT_EQ(1u, mailboxManager->count());
   EXPECT_TRUE(ETCH_OK == message->getMessageId(id));
@@ -256,9 +256,8 @@
 
   //wait for the response
   capu::SmartPointer<EtchObject> result;
-  status = deliveryService->endcall(mail, replyType, result);
+  status = deliveryService->endcall(mail.get(), replyType, result);
   EXPECT_EQ(ETCH_OK, status);
-  delete mail;
 
   //check the result
   EXPECT_TRUE(result->equals(data.get()));
diff --git a/binding-cpp/runtime/src/test/transport/EtchPlainMailboxManagerTest.cpp b/binding-cpp/runtime/src/test/transport/EtchPlainMailboxManagerTest.cpp
index ab6cc0a..67ca445 100644
--- a/binding-cpp/runtime/src/test/transport/EtchPlainMailboxManagerTest.cpp
+++ b/binding-cpp/runtime/src/test/transport/EtchPlainMailboxManagerTest.cpp
@@ -157,7 +157,7 @@
   EXPECT_TRUE(ETCH_OK == message->getMessageId(id));
   EXPECT_TRUE(ETCH_OK != message->getInReplyToMessageId(id));
 
-  EtchMailbox *mail;
+  capu::SmartPointer<EtchMailbox> mail;
   EXPECT_TRUE(ETCH_OK != manager->getMailbox(id, mail));
 
   message->clear();
@@ -201,7 +201,7 @@
   EXPECT_TRUE(ETCH_OK == message->getMessageId(id));
   EXPECT_TRUE(ETCH_OK == message->getInReplyToMessageId(id));
   EXPECT_TRUE(id == 1L);
-  EtchMailbox *mail;
+  capu::SmartPointer<EtchMailbox> mail;
   EXPECT_TRUE(ETCH_OK != manager->getMailbox(id, mail));
 
   message->clear();
@@ -239,13 +239,14 @@
   //in order to notify upper layers that the connection is open
   manager->sessionNotify(new EtchString(EtchSession::UP()));
 
-  EtchMailbox *mail;
+  capu::SmartPointer<EtchMailbox> mail;
   EXPECT_TRUE(ETCH_OK == manager->transportCall(NULL, message, mail));
   EXPECT_EQ(1u, manager->count());
   EXPECT_TRUE(ETCH_OK == message->getMessageId(id));
   EXPECT_TRUE(ETCH_OK != message->getInReplyToMessageId(id));
 
-  EXPECT_TRUE(ETCH_OK == manager->getMailbox(id, mail));
+  capu::SmartPointer<EtchMailbox> out;
+  EXPECT_TRUE(ETCH_OK == manager->getMailbox(id, out));
 
   message->clear();
 
@@ -285,7 +286,7 @@
   //in order to notify upper layers that the connection is open
   manager->sessionNotify(new EtchString(EtchSession::UP()));
 
-  EtchMailbox *mail;
+  capu::SmartPointer<EtchMailbox> mail;
   EXPECT_TRUE(ETCH_ERROR == manager->transportCall(NULL, message, mail));
   //should not create a mailbox
   EXPECT_EQ(ETCH_OK, message->getMessageId(id));
@@ -333,7 +334,7 @@
   manager->sessionNotify(new EtchString(EtchSession::UP()));
 
   //perform the call
-  EtchMailbox *mail;
+  capu::SmartPointer<EtchMailbox> mail;
   EXPECT_TRUE(ETCH_OK == manager->transportCall(NULL, message, mail));
   EXPECT_EQ(1u, manager->count());
   EXPECT_TRUE(ETCH_OK == message->getMessageId(id));