Author: nmittler Date: Sat Dec 16 07:56:43 2006 New Revision: 487819 URL: http://svn.apache.org/viewvc?view=rev&rev=487819 Log: [AMQCPP-26] Updates to fix memory leaks detected in unit tests by rational purify
Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/core/ActiveMQConnection.cpp incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/core/ActiveMQConnectionData.h incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/IOTransport.h incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/core/ActiveMQConnectionFactoryTest.h incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/transport/IOTransportTest.h Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/core/ActiveMQConnection.cpp URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/core/ActiveMQConnection.cpp?view=diff&rev=487819&r1=487818&r2=487819 ============================================================================== --- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/core/ActiveMQConnection.cpp (original) +++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/core/ActiveMQConnection.cpp Sat Dec 16 07:56:43 2006 @@ -101,13 +101,12 @@ started = false; closed = true; - // Shutdown connector and transport - connectionData->getConnector()->close(); - connectionData->getTransport()->close(); - - // Destroy the connection data - delete connectionData; - connectionData = NULL; + // Destroy the connection data. This will close the connector + // and transports. + if( connectionData != NULL ){ + delete connectionData; + connectionData = NULL; + } } AMQ_CATCH_RETHROW( ActiveMQException ) AMQ_CATCHALL_THROW( ActiveMQException ) Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/core/ActiveMQConnectionData.h URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/core/ActiveMQConnectionData.h?view=diff&rev=487819&r1=487818&r2=487819 ============================================================================== --- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/core/ActiveMQConnectionData.h (original) +++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/core/ActiveMQConnectionData.h Sat Dec 16 07:56:43 2006 @@ -72,17 +72,75 @@ this->properties = properties; } - virtual ~ActiveMQConnectionData(void) - { + virtual ~ActiveMQConnectionData(){ + close(); + } + + virtual void close() throw (exceptions::ActiveMQException) { + + bool hasException = false; + exceptions::ActiveMQException e; + try { - connector->close(); - delete connector; + if( connector != NULL ){ + + try{ + connector->close(); + }catch( exceptions::ActiveMQException& ex ){ + if( !hasException ){ + hasException = true; + ex.setMark(__FILE__, __LINE__ ); + e = ex; + } + } + + try{ + delete connector; + }catch( exceptions::ActiveMQException& ex ){ + if( !hasException ){ + hasException = true; + ex.setMark(__FILE__, __LINE__ ); + e = ex; + } + } + connector = NULL; + } + + if( transport != NULL ){ + + try{ + transport->close(); + }catch( exceptions::ActiveMQException& ex ){ + if( !hasException ){ + hasException = true; + ex.setMark(__FILE__, __LINE__ ); + e = ex; + } + } + + try{ + delete transport; + }catch( exceptions::ActiveMQException& ex ){ + if( !hasException ){ + hasException = true; + ex.setMark(__FILE__, __LINE__ ); + e = ex; + } + } + transport = NULL; + } - transport->close(); - delete transport; + if( properties != NULL ){ + delete properties; + properties = NULL; + } - delete properties; + // If we encountered an exception - throw the first + // one we encountered. + if( hasException ){ + throw e; + } } AMQ_CATCH_NOTHROW( exceptions::ActiveMQException ) AMQ_CATCHALL_NOTHROW( ) @@ -92,7 +150,7 @@ * Get the Connector that this Connection Data object holds * @return Connector Pointer */ - virtual connector::Connector* getConnector(void){ + virtual connector::Connector* getConnector(){ return connector; } @@ -100,7 +158,7 @@ * Get the Connector that this Connection Data object holds * @return Connector Pointer */ - virtual transport::Transport* getTransport(void){ + virtual transport::Transport* getTransport(){ return transport; } @@ -109,7 +167,7 @@ * this Connection. * @return Properties object reference. */ - virtual const util::Properties& getProperties(void) const { + virtual const util::Properties& getProperties() const { return *properties; } Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/IOTransport.h URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/IOTransport.h?view=diff&rev=487819&r1=487818&r2=487819 ============================================================================== --- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/IOTransport.h (original) +++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/IOTransport.h Sat Dec 16 07:56:43 2006 @@ -115,9 +115,15 @@ void fire( Command* command ){ try{ - if( listener != NULL ){ - listener->onCommand( command ); + // Since the listener is responsible for freeing the memory, + // if there is no listener - free the command here. + if( listener == NULL ){ + delete command; + return; } + + listener->onCommand( command ); + }catch( ... ){} } Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/core/ActiveMQConnectionFactoryTest.h URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/core/ActiveMQConnectionFactoryTest.h?view=diff&rev=487819&r1=487818&r2=487819 ============================================================================== --- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/core/ActiveMQConnectionFactoryTest.h (original) +++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/core/ActiveMQConnectionFactoryTest.h Sat Dec 16 07:56:43 2006 @@ -114,6 +114,9 @@ CPPUNIT_ASSERT( password == connector->getPassword() ); CPPUNIT_ASSERT( clientId == connector->getClientId() ); + // Free the allocated connection object. + delete connection; + return; } AMQ_CATCH_NOTHROW( exceptions::ActiveMQException ) Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/transport/IOTransportTest.h URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/transport/IOTransportTest.h?view=diff&rev=487819&r1=487818&r2=487819 ============================================================================== --- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/transport/IOTransportTest.h (original) +++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/transport/IOTransportTest.h Sat Dec 16 07:56:43 2006 @@ -105,7 +105,17 @@ synchronized( inputStream ){ MyCommand* command = new MyCommand(); - command->c = inputStream->read(); + try{ + command->c = inputStream->read(); + } catch( exceptions::ActiveMQException& ex ){ + + // Free the memory. + delete command; + + ex.setMark( __FILE__, __LINE__ ); + throw ex; + } + return command; }