You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2008/11/19 15:32:25 UTC

svn commit: r718973 - /activemq/activemq-cpp/trunk/src/examples/main.cpp

Author: tabish
Date: Wed Nov 19 06:32:25 2008
New Revision: 718973

URL: http://svn.apache.org/viewvc?rev=718973&view=rev
Log:
Fixed a leak that can occur when run without a broker running

Modified:
    activemq/activemq-cpp/trunk/src/examples/main.cpp

Modified: activemq/activemq-cpp/trunk/src/examples/main.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/examples/main.cpp?rev=718973&r1=718972&r2=718973&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/examples/main.cpp (original)
+++ activemq/activemq-cpp/trunk/src/examples/main.cpp Wed Nov 19 06:32:25 2008
@@ -33,6 +33,7 @@
 #include <cms/MessageListener.h>
 #include <stdlib.h>
 #include <iostream>
+#include <memory>
 
 using namespace activemq::core;
 using namespace decaf::util::concurrent;
@@ -75,21 +76,15 @@
 
     virtual void run() {
 
-        ConnectionFactory* connectionFactory = NULL;
-
         try {
             // Create a ConnectionFactory
-            ConnectionFactory* connectionFactory =
-                ConnectionFactory::createCMSConnectionFactory( brokerURI );
+            auto_ptr<ConnectionFactory> connectionFactory(
+                ConnectionFactory::createCMSConnectionFactory( brokerURI ) );
 
             // Create a Connection
             connection = connectionFactory->createConnection();
             connection->start();
 
-            // free the factory, we are done with it.
-            delete connectionFactory;
-            connectionFactory = NULL;
-
             // Create a Session
             if( this->sessionTransacted ) {
                 session = connection->createSession( Session::SESSION_TRANSACTED );
@@ -127,9 +122,6 @@
             }
 
         }catch ( CMSException& e ) {
-            delete connectionFactory;
-            connectionFactory = NULL;
-
             e.printStackTrace();
         }
     }
@@ -138,32 +130,32 @@
 
     void cleanup(){
 
-            // Destroy resources.
-            try{
-                if( destination != NULL ) delete destination;
-            }catch ( CMSException& e ) { e.printStackTrace(); }
-            destination = NULL;
-
-            try{
-                if( producer != NULL ) delete producer;
-            }catch ( CMSException& e ) { e.printStackTrace(); }
-            producer = NULL;
-
-            // Close open resources.
-            try{
-                if( session != NULL ) session->close();
-                if( connection != NULL ) connection->close();
-            }catch ( CMSException& e ) { e.printStackTrace(); }
-
-            try{
-                if( session != NULL ) delete session;
-            }catch ( CMSException& e ) { e.printStackTrace(); }
-            session = NULL;
-
-            try{
-                if( connection != NULL ) delete connection;
-            }catch ( CMSException& e ) { e.printStackTrace(); }
-            connection = NULL;
+        // Destroy resources.
+        try{
+            if( destination != NULL ) delete destination;
+        }catch ( CMSException& e ) { e.printStackTrace(); }
+        destination = NULL;
+
+        try{
+            if( producer != NULL ) delete producer;
+        }catch ( CMSException& e ) { e.printStackTrace(); }
+        producer = NULL;
+
+        // Close open resources.
+        try{
+            if( session != NULL ) session->close();
+            if( connection != NULL ) connection->close();
+        }catch ( CMSException& e ) { e.printStackTrace(); }
+
+        try{
+            if( session != NULL ) delete session;
+        }catch ( CMSException& e ) { e.printStackTrace(); }
+        session = NULL;
+
+        try{
+            if( connection != NULL ) delete connection;
+        }catch ( CMSException& e ) { e.printStackTrace(); }
+        connection = NULL;
     }
 };
 
@@ -211,22 +203,15 @@
 
     virtual void run() {
 
-        ConnectionFactory* connectionFactory = NULL;
-
         try {
 
             // Create a ConnectionFactory
-            connectionFactory =
-                ConnectionFactory::createCMSConnectionFactory( brokerURI );
+            auto_ptr<ConnectionFactory> connectionFactory(
+                ConnectionFactory::createCMSConnectionFactory( brokerURI ) );
 
             // Create a Connection
             connection = connectionFactory->createConnection();
-
-            delete connectionFactory;
-            connectionFactory = NULL;
-
             connection->start();
-
             connection->setExceptionListener(this);
 
             // Create a Session
@@ -257,14 +242,11 @@
             // Wait while asynchronous messages come in.
             doneLatch.await( waitMillis );
 
-        } catch (CMSException& e) {
+        } catch( CMSException& e ) {
 
             // Indicate we are ready for messages.
             latch.countDown();
 
-            delete connectionFactory;
-            connectionFactory = NULL;
-
             e.printStackTrace();
         }
     }