From 1edd3679288c7582e85c5c033d6c7088651166a0 Mon Sep 17 00:00:00 2001 From: Takeshi Abe Date: Fri, 4 Jul 2014 23:05:30 +0900 Subject: Avoid possible memory leaks in case of exceptions Change-Id: I957c0662c56e7cad3d10c8bcc1d6b22352da688f --- svtools/source/filter/SvFilterOptionsDialog.cxx | 7 ++--- svtools/source/graphic/descriptor.cxx | 11 +++----- svtools/source/graphic/grfmgr.cxx | 9 +++---- svtools/source/graphic/grfmgr2.cxx | 36 +++++++++---------------- svtools/source/graphic/provider.cxx | 20 ++++++-------- 5 files changed, 31 insertions(+), 52 deletions(-) (limited to 'svtools') diff --git a/svtools/source/filter/SvFilterOptionsDialog.cxx b/svtools/source/filter/SvFilterOptionsDialog.cxx index c8267ccdb81d..f3321aa72b97 100644 --- a/svtools/source/filter/SvFilterOptionsDialog.cxx +++ b/svtools/source/filter/SvFilterOptionsDialog.cxx @@ -46,6 +46,7 @@ #include #include #include +#include using namespace ::com::sun::star; @@ -249,15 +250,15 @@ sal_Int16 SvFilterOptionsDialog::execute() FltCallDialogParameter aFltCallDlgPara( Application::GetDefDialogParent(), NULL, meFieldUnit ); aFltCallDlgPara.aFilterData = maFilterDataSequence; - ResMgr* pResMgr = ResMgr::CreateResMgr( "svt", Application::GetSettings().GetUILanguageTag() ); - aFltCallDlgPara.pResMgr = pResMgr; + boost::scoped_ptr pResMgr(ResMgr::CreateResMgr( "svt", Application::GetSettings().GetUILanguageTag() )); + aFltCallDlgPara.pResMgr = pResMgr.get(); aFltCallDlgPara.aFilterExt = aGraphicFilter.GetExportFormatShortName( nFormat ); bool bIsPixelFormat( aGraphicFilter.IsExportPixelFormat( nFormat ) ); if ( ExportDialog( aFltCallDlgPara, mxContext, mxSourceDocument, mbExportSelection, bIsPixelFormat ).Execute() == RET_OK ) nRet = ui::dialogs::ExecutableDialogResults::OK; - delete pResMgr; + pResMgr.reset(); // taking the out parameter from the dialog maFilterDataSequence = aFltCallDlgPara.aFilterData; diff --git a/svtools/source/graphic/descriptor.cxx b/svtools/source/graphic/descriptor.cxx index d24911d60e25..1114923dc197 100644 --- a/svtools/source/graphic/descriptor.cxx +++ b/svtools/source/graphic/descriptor.cxx @@ -33,6 +33,7 @@ #include #include +#include #define UNOGRAPHIC_GRAPHICTYPE 1 #define UNOGRAPHIC_MIMETYPE 2 @@ -82,13 +83,10 @@ void GraphicDescriptor::init( const ::Graphic& rGraphic ) void GraphicDescriptor::init( const OUString& rURL ) throw() { - SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( rURL, STREAM_READ ); + boost::scoped_ptr pIStm(::utl::UcbStreamHelper::CreateStream( rURL, STREAM_READ )); if( pIStm ) - { implCreate( *pIStm, &rURL ); - delete pIStm; - } } @@ -96,13 +94,10 @@ void GraphicDescriptor::init( const OUString& rURL ) void GraphicDescriptor::init( const uno::Reference< io::XInputStream >& rxIStm, const OUString& rURL ) throw() { - SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( rxIStm ); + boost::scoped_ptr pIStm(::utl::UcbStreamHelper::CreateStream( rxIStm )); if( pIStm ) - { implCreate( *pIStm, &rURL ); - delete pIStm; - } } diff --git a/svtools/source/graphic/grfmgr.cxx b/svtools/source/graphic/grfmgr.cxx index 10130cdda8e3..408081ebc40f 100644 --- a/svtools/source/graphic/grfmgr.cxx +++ b/svtools/source/graphic/grfmgr.cxx @@ -37,6 +37,7 @@ #include #include +#include using com::sun::star::uno::Reference; using com::sun::star::uno::XInterface; @@ -210,13 +211,12 @@ void GraphicObject::ImplAutoSwapIn() if( ::utl::LocalFileHelper::ConvertPhysicalNameToURL( GetLink(), aURLStr ) ) { - SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( aURLStr, STREAM_READ ); + boost::scoped_ptr pIStm(::utl::UcbStreamHelper::CreateStream( aURLStr, STREAM_READ )); if( pIStm ) { ReadGraphic( *pIStm, maGraphic ); mbAutoSwapped = ( maGraphic.GetType() != GRAPHIC_NONE ); - delete pIStm; } } } @@ -1218,12 +1218,9 @@ GraphicObject GraphicObject::CreateGraphicObjectFromURL( const OUString &rURL ) Graphic aGraphic; if ( !aURL.isEmpty() ) { - SvStream* pStream = utl::UcbStreamHelper::CreateStream( aURL, STREAM_READ ); + boost::scoped_ptr pStream(utl::UcbStreamHelper::CreateStream( aURL, STREAM_READ )); if( pStream ) - { GraphicConverter::Import( *pStream, aGraphic ); - delete pStream; - } } return GraphicObject( aGraphic ); diff --git a/svtools/source/graphic/grfmgr2.cxx b/svtools/source/graphic/grfmgr2.cxx index 295b4e05faf1..117a4d0121e7 100644 --- a/svtools/source/graphic/grfmgr2.cxx +++ b/svtools/source/graphic/grfmgr2.cxx @@ -33,7 +33,7 @@ #include #include "grfcache.hxx" #include - +#include // - defines - @@ -275,10 +275,10 @@ bool ImplCreateRotatedScaled( const BitmapEx& rBmpEx, const GraphicAttr& rAttrib bool bHMirr = ( rAttributes.GetMirrorFlags() & BMP_MIRROR_HORZ ) != 0; bool bVMirr = ( rAttributes.GetMirrorFlags() & BMP_MIRROR_VERT ) != 0; - long* pMapIX = new long[ aUnrotatedWidth ]; - long* pMapFX = new long[ aUnrotatedWidth ]; - long* pMapIY = new long[ aUnrotatedHeight ]; - long* pMapFY = new long[ aUnrotatedHeight ]; + boost::scoped_array pMapIX(new long[ aUnrotatedWidth ]); + boost::scoped_array pMapFX(new long[ aUnrotatedWidth ]); + boost::scoped_array pMapIY(new long[ aUnrotatedHeight ]); + boost::scoped_array pMapFY(new long[ aUnrotatedHeight ]); double fRevScaleX; double fRevScaleY; @@ -372,10 +372,10 @@ bool ImplCreateRotatedScaled( const BitmapEx& rBmpEx, const GraphicAttr& rAttrib const double fSinAngle = sin( nRot10 * F_PI1800 ); const long aTargetWidth = nEndX - nStartX + 1L; const long aTargetHeight = nEndY - nStartY + 1L; - long* pCosX = new long[ aTargetWidth ]; - long* pSinX = new long[ aTargetWidth ]; - long* pCosY = new long[ aTargetHeight ]; - long* pSinY = new long[ aTargetHeight ]; + boost::scoped_array pCosX(new long[ aTargetWidth ]); + boost::scoped_array pSinX(new long[ aTargetWidth ]); + boost::scoped_array pCosY(new long[ aTargetHeight ]); + boost::scoped_array pSinY(new long[ aTargetHeight ]); long nUnRotX, nUnRotY, nSinY, nCosY; sal_uInt8 cR0, cG0, cB0, cR1, cG1, cB1; bool bRet = false; @@ -739,8 +739,8 @@ bool ImplCreateRotatedScaled( const BitmapEx& rBmpEx, const GraphicAttr& rAttrib if( !aMsk || ( ( pMAcc = aMsk.AcquireReadAccess() ) != NULL ) ) { - long* pMapLX = new long[ aUnrotatedWidth ]; - long* pMapLY = new long[ aUnrotatedHeight ]; + boost::scoped_array pMapLX(new long[ aUnrotatedWidth ]); + boost::scoped_array pMapLY(new long[ aUnrotatedHeight ]); BitmapColor aTestB; if( pMAcc ) @@ -783,8 +783,8 @@ bool ImplCreateRotatedScaled( const BitmapEx& rBmpEx, const GraphicAttr& rAttrib } } - delete[] pMapLX; - delete[] pMapLY; + pMapLX.reset(); + pMapLY.reset(); if( pMAcc ) aMsk.ReleaseAccess( pMAcc ); @@ -805,16 +805,6 @@ bool ImplCreateRotatedScaled( const BitmapEx& rBmpEx, const GraphicAttr& rAttrib else rOutBmpEx = aOutBmp; - delete[] pSinX; - delete[] pCosX; - delete[] pSinY; - delete[] pCosY; - - delete[] pMapIX; - delete[] pMapFX; - delete[] pMapIY; - delete[] pMapFY; - return bRet; } diff --git a/svtools/source/graphic/provider.cxx b/svtools/source/graphic/provider.cxx index 8a1295b71854..1cf6620820d2 100644 --- a/svtools/source/graphic/provider.cxx +++ b/svtools/source/graphic/provider.cxx @@ -43,6 +43,7 @@ #include #include "provider.hxx" #include +#include using namespace com::sun::star; @@ -236,7 +237,7 @@ uno::Reference< ::graphic::XGraphic > GraphicProvider::implLoadResource( const O OString aResMgrName(OUStringToOString( rResourceURL.getToken(0, '/', nIndex), RTL_TEXTENCODING_ASCII_US)); - ResMgr* pResMgr = ResMgr::CreateResMgr( aResMgrName.getStr(), Application::GetSettings().GetUILanguageTag() ); + boost::scoped_ptr pResMgr(ResMgr::CreateResMgr( aResMgrName.getStr(), Application::GetSettings().GetUILanguageTag() )); if( pResMgr ) { @@ -296,8 +297,6 @@ uno::Reference< ::graphic::XGraphic > GraphicProvider::implLoadResource( const O xRet = pUnoGraphic; } } - - delete pResMgr; } } @@ -384,7 +383,7 @@ uno::Reference< ::graphic::XGraphic > SAL_CALL GraphicProvider::queryGraphic( co { uno::Reference< ::graphic::XGraphic > xRet; OUString aPath; - SvStream* pIStm = NULL; + boost::scoped_ptr pIStm; uno::Reference< io::XInputStream > xIStm; uno::Reference< awt::XBitmap >xBtm; @@ -443,7 +442,7 @@ uno::Reference< ::graphic::XGraphic > SAL_CALL GraphicProvider::queryGraphic( co if( xIStm.is() ) { - pIStm = ::utl::UcbStreamHelper::CreateStream( xIStm ); + pIStm.reset(::utl::UcbStreamHelper::CreateStream( xIStm )); } else if( !aPath.isEmpty() ) { @@ -462,7 +461,7 @@ uno::Reference< ::graphic::XGraphic > SAL_CALL GraphicProvider::queryGraphic( co xRet = implLoadStandardImage( aPath ); if( !xRet.is() ) - pIStm = ::utl::UcbStreamHelper::CreateStream( aPath, STREAM_READ ); + pIStm.reset(::utl::UcbStreamHelper::CreateStream( aPath, STREAM_READ )); } else if( xBtm.is() ) { @@ -495,8 +494,6 @@ uno::Reference< ::graphic::XGraphic > SAL_CALL GraphicProvider::queryGraphic( co xRet = pUnoGraphic; } } - - delete pIStm; } return xRet; @@ -729,7 +726,7 @@ void SAL_CALL GraphicProvider::storeGraphic( const uno::Reference< ::graphic::XG { SolarMutexGuard g; - SvStream* pOStm = NULL; + boost::scoped_ptr pOStm; OUString aPath; sal_Int32 i; @@ -743,7 +740,7 @@ void SAL_CALL GraphicProvider::storeGraphic( const uno::Reference< ::graphic::XG OUString aURL; aValue >>= aURL; - pOStm = ::utl::UcbStreamHelper::CreateStream( aURL, STREAM_WRITE | STREAM_TRUNC ); + pOStm.reset(::utl::UcbStreamHelper::CreateStream( aURL, STREAM_WRITE | STREAM_TRUNC )); aPath = aURL; } else if (aName == "OutputStream") @@ -753,7 +750,7 @@ void SAL_CALL GraphicProvider::storeGraphic( const uno::Reference< ::graphic::XG aValue >>= xOStm; if( xOStm.is() ) - pOStm = ::utl::UcbStreamHelper::CreateStream( xOStm ); + pOStm.reset(::utl::UcbStreamHelper::CreateStream( xOStm )); } } @@ -846,7 +843,6 @@ void SAL_CALL GraphicProvider::storeGraphic( const uno::Reference< ::graphic::XG } } } - delete pOStm; } } -- cgit