From 57f5f399c6047fa6c484a3b969c84aca3a723fab Mon Sep 17 00:00:00 2001 From: Noel Date: Wed, 3 Feb 2021 08:59:13 +0200 Subject: use more unique_ptr in EnhancedCustomShape2d and fix some leaks in the unit tests Change-Id: I8d324a24de11b14b4820e3cdd7f078d5877489e3 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/110288 Tested-by: Jenkins Reviewed-by: Noel Grandin --- svx/qa/unit/customshapes.cxx | 12 ++- svx/source/customshapes/EnhancedCustomShape2d.cxx | 111 +++++++++------------ .../customshapes/EnhancedCustomShapeEngine.cxx | 4 +- 3 files changed, 57 insertions(+), 70 deletions(-) (limited to 'svx') diff --git a/svx/qa/unit/customshapes.cxx b/svx/qa/unit/customshapes.cxx index d0d4ac3d3e6d..271a7aad6f19 100644 --- a/svx/qa/unit/customshapes.cxx +++ b/svx/qa/unit/customshapes.cxx @@ -349,7 +349,8 @@ CPPUNIT_TEST_FIXTURE(CustomshapesTest, testTdf121845_two_commands_U) SdrObjCustomShape& rSdrObjCustomShape( static_cast(*GetSdrObjectFromXShape(xShape))); EnhancedCustomShape2d aCustomShape2d(rSdrObjCustomShape); - SdrPathObj* pPathObj = static_cast(aCustomShape2d.CreateLineGeometry()); + SdrPathObjUniquePtr pPathObj( + static_cast(aCustomShape2d.CreateLineGeometry().release())); CPPUNIT_ASSERT_MESSAGE("Could not convert to SdrPathObj", pPathObj); const basegfx::B2DPolyPolygon aPolyPolygon(pPathObj->GetPathPoly()); CPPUNIT_ASSERT_EQUAL_MESSAGE("count polygons", static_cast(2), @@ -777,7 +778,8 @@ CPPUNIT_TEST_FIXTURE(CustomshapesTest, testTdf103474_commandT_CaseZeroHeight) SdrObjCustomShape& rSdrObjCustomShape( static_cast(*GetSdrObjectFromXShape(xShape))); EnhancedCustomShape2d aCustomShape2d(rSdrObjCustomShape); - SdrPathObj* pPathObj = static_cast(aCustomShape2d.CreateLineGeometry()); + SdrPathObjUniquePtr pPathObj( + static_cast(aCustomShape2d.CreateLineGeometry().release())); CPPUNIT_ASSERT_MESSAGE("Could not convert to SdrPathObj", pPathObj); const basegfx::B2DPolyPolygon aPolyPolygon(pPathObj->GetPathPoly()); CPPUNIT_ASSERT_EQUAL_MESSAGE("count polygons", static_cast(1), @@ -807,7 +809,8 @@ CPPUNIT_TEST_FIXTURE(CustomshapesTest, testTdf103474_commandG_CaseZeroHeight) SdrObjCustomShape& rSdrObjCustomShape( static_cast(*GetSdrObjectFromXShape(xShape))); EnhancedCustomShape2d aCustomShape2d(rSdrObjCustomShape); - SdrPathObj* pPathObj = static_cast(aCustomShape2d.CreateLineGeometry()); + SdrPathObjUniquePtr pPathObj( + static_cast(aCustomShape2d.CreateLineGeometry().release())); CPPUNIT_ASSERT_MESSAGE("Could not convert to SdrPathObj", pPathObj); const basegfx::B2DPolyPolygon aPolyPolygon(pPathObj->GetPathPoly()); CPPUNIT_ASSERT_EQUAL_MESSAGE("count polygons", static_cast(1), @@ -837,7 +840,8 @@ CPPUNIT_TEST_FIXTURE(CustomshapesTest, testTdf122323_largeSwingAngle) SdrObjCustomShape& rSdrObjCustomShape( static_cast(*GetSdrObjectFromXShape(xShape))); EnhancedCustomShape2d aCustomShape2d(rSdrObjCustomShape); - SdrPathObj* pPathObj = static_cast(aCustomShape2d.CreateLineGeometry()); + SdrPathObjUniquePtr pPathObj( + static_cast(aCustomShape2d.CreateLineGeometry().release())); CPPUNIT_ASSERT_MESSAGE("Could not convert to SdrPathObj", pPathObj); const basegfx::B2DPolyPolygon aPolyPolygon(pPathObj->GetPathPoly()); const basegfx::B2DPolygon aPolygon(aPolyPolygon.getB2DPolygon(0)); diff --git a/svx/source/customshapes/EnhancedCustomShape2d.cxx b/svx/source/customshapes/EnhancedCustomShape2d.cxx index 57d622eb6acc..32c458df7848 100644 --- a/svx/source/customshapes/EnhancedCustomShape2d.cxx +++ b/svx/source/customshapes/EnhancedCustomShape2d.cxx @@ -2020,7 +2020,7 @@ static double lcl_getNormalizedAngleRad(const double fCircleAngleDeg) void EnhancedCustomShape2d::CreateSubPath( sal_Int32& rSrcPt, sal_Int32& rSegmentInd, - std::vector< std::pair< SdrPathObj*, double> >& rObjectList, + std::vector< std::pair< SdrPathObjUniquePtr, double> >& rObjectList, const bool bLineGeometryNeededOnly, const bool bSortFilledObjectsToBack, sal_Int32 nIndex) @@ -2579,15 +2579,15 @@ void EnhancedCustomShape2d::CreateSubPath( { basegfx::B2DPolyPolygon aClosedPolyPolygon(aNewB2DPolyPolygon); aClosedPolyPolygon.setClosed(true); - SdrPathObj* pFill = new SdrPathObj( + SdrPathObjUniquePtr pFill(new SdrPathObj( mrSdrObjCustomShape.getSdrModelFromSdrObject(), OBJ_POLY, - aClosedPolyPolygon); + aClosedPolyPolygon)); SfxItemSet aTempSet(*this); aTempSet.Put(makeSdrShadowItem(false)); aTempSet.Put(XLineStyleItem(drawing::LineStyle_NONE)); pFill->SetMergedItemSet(aTempSet); - rObjectList.push_back(std::pair< SdrPathObj*, double >(pFill, dBrightness)); + rObjectList.push_back(std::pair< SdrPathObjUniquePtr, double >(std::move(pFill), dBrightness)); } if(!bNoStroke) @@ -2596,39 +2596,39 @@ void EnhancedCustomShape2d::CreateSubPath( // the non-fill is defined by XFILL_NONE. Since SdrPathObj::ImpForceKind() needs // to correct the polygon (here: open it) using the type, the last edge may get lost. // Thus, use a type that fits the polygon - SdrPathObj* pStroke = new SdrPathObj( + SdrPathObjUniquePtr pStroke(new SdrPathObj( mrSdrObjCustomShape.getSdrModelFromSdrObject(), aNewB2DPolyPolygon.isClosed() ? OBJ_POLY : OBJ_PLIN, - aNewB2DPolyPolygon); + aNewB2DPolyPolygon)); SfxItemSet aTempSet(*this); aTempSet.Put(makeSdrShadowItem(false)); aTempSet.Put(XFillStyleItem(drawing::FillStyle_NONE)); pStroke->SetMergedItemSet(aTempSet); - rObjectList.push_back(std::pair< SdrPathObj*, double >(pStroke, dBrightness)); + rObjectList.push_back(std::pair< SdrPathObjUniquePtr, double >(std::move(pStroke), dBrightness)); } } else { - SdrPathObj* pObj = nullptr; + SdrPathObjUniquePtr pObj; SfxItemSet aTempSet(*this); aTempSet.Put(makeSdrShadowItem(false)); if(bNoFill) { // see comment above about OBJ_PLIN - pObj = new SdrPathObj( + pObj.reset(new SdrPathObj( mrSdrObjCustomShape.getSdrModelFromSdrObject(), aNewB2DPolyPolygon.isClosed() ? OBJ_POLY : OBJ_PLIN, - aNewB2DPolyPolygon); + aNewB2DPolyPolygon)); aTempSet.Put(XFillStyleItem(drawing::FillStyle_NONE)); } else { aNewB2DPolyPolygon.setClosed(true); - pObj = new SdrPathObj( + pObj.reset(new SdrPathObj( mrSdrObjCustomShape.getSdrModelFromSdrObject(), OBJ_POLY, - aNewB2DPolyPolygon); + aNewB2DPolyPolygon)); } if(bNoStroke) @@ -2637,14 +2637,14 @@ void EnhancedCustomShape2d::CreateSubPath( } pObj->SetMergedItemSet(aTempSet); - rObjectList.push_back(std::pair< SdrPathObj*, double >(pObj, dBrightness)); + rObjectList.push_back(std::pair< SdrPathObjUniquePtr, double >(std::move(pObj), dBrightness)); } } static void CorrectCalloutArrows( MSO_SPT eSpType, sal_uInt32 nLineObjectCount, - std::vector< std::pair< SdrPathObj*, double> >& vObjectList ) + std::vector< std::pair< SdrPathObjUniquePtr, double> >& vObjectList ) { bool bAccent = false; switch( eSpType ) @@ -2663,9 +2663,9 @@ static void CorrectCalloutArrows( { sal_uInt32 nLine = 0; - for ( const std::pair< SdrPathObj*, double >& rCandidate : vObjectList ) + for ( const std::pair< SdrPathObjUniquePtr, double >& rCandidate : vObjectList ) { - SdrPathObj* pObj(rCandidate.first); + SdrPathObj* pObj(rCandidate.first.get()); if(pObj->IsLine()) { @@ -2690,9 +2690,9 @@ static void CorrectCalloutArrows( { sal_uInt32 nLine = 0; - for ( const std::pair< SdrPathObj*, double >& rCandidate : vObjectList ) + for ( const std::pair< SdrPathObjUniquePtr, double >& rCandidate : vObjectList ) { - SdrPathObj* pObj(rCandidate.first); + SdrPathObj* pObj(rCandidate.first.get()); if(pObj->IsLine()) { @@ -2718,9 +2718,9 @@ static void CorrectCalloutArrows( { sal_uInt32 nLine = 0; - for ( const std::pair< SdrPathObj*, double >& rCandidate : vObjectList ) + for ( const std::pair< SdrPathObjUniquePtr, double >& rCandidate : vObjectList ) { - SdrPathObj* pObj(rCandidate.first); + SdrPathObj* pObj(rCandidate.first.get()); if(pObj->IsLine()) { @@ -2824,19 +2824,19 @@ void EnhancedCustomShape2d::AdaptObjColor( nColorIndex++; } -SdrObject* EnhancedCustomShape2d::CreatePathObj( bool bLineGeometryNeededOnly ) +SdrObjectUniquePtr EnhancedCustomShape2d::CreatePathObj( bool bLineGeometryNeededOnly ) { if ( !seqCoordinates.hasElements() ) { return nullptr; } - std::vector< std::pair< SdrPathObj*, double > > vObjectList; + std::vector< std::pair< SdrPathObjUniquePtr, double > > vObjectList; const bool bSortFilledObjectsToBack(SortFilledObjectsToBackByDefault(eSpType)); sal_Int32 nSubPathIndex(0); sal_Int32 nSrcPt(0); sal_Int32 nSegmentInd(0); - SdrObject* pRet(nullptr); + SdrObjectUniquePtr pRet; while( nSegmentInd <= seqSegments.getLength() ) { @@ -2857,28 +2857,20 @@ SdrObject* EnhancedCustomShape2d::CreatePathObj( bool bLineGeometryNeededOnly ) sal_uInt32 nColorIndex(0); // #i37011# remove invisible objects - std::vector< std::pair< SdrPathObj*, double> > vNewList; + std::vector< std::pair< SdrPathObjUniquePtr, double> > vNewList; - for ( const std::pair< SdrPathObj*, double >& rCandidate : vObjectList ) + for ( std::pair< SdrPathObjUniquePtr, double >& rCandidate : vObjectList ) { - SdrPathObj* pObj(rCandidate.first); + SdrPathObj* pObj(rCandidate.first.get()); const drawing::LineStyle eLineStyle(pObj->GetMergedItem(XATTR_LINESTYLE).GetValue()); const drawing::FillStyle eFillStyle(pObj->GetMergedItem(XATTR_FILLSTYLE).GetValue()); // #i40600# if bLineGeometryNeededOnly is set, linestyle does not matter - if(!bLineGeometryNeededOnly && (drawing::LineStyle_NONE == eLineStyle) && (drawing::FillStyle_NONE == eFillStyle)) - { - // always use SdrObject::Free(...) for SdrObjects (!) - SdrObject* pTemp(pObj); - SdrObject::Free(pTemp); - } - else - { - vNewList.push_back(rCandidate); - } + if(bLineGeometryNeededOnly || (drawing::LineStyle_NONE != eLineStyle) || (drawing::FillStyle_NONE != eFillStyle)) + vNewList.push_back(std::move(rCandidate)); } - vObjectList = vNewList; + vObjectList = std::move(vNewList); if(1 == vObjectList.size()) { @@ -2895,9 +2887,9 @@ SdrObject* EnhancedCustomShape2d::CreatePathObj( bool bLineGeometryNeededOnly ) sal_Int32 nLineObjectCount(0); // correct some values and collect content data - for ( const std::pair< SdrPathObj*, double >& rCandidate : vObjectList ) + for ( const std::pair< SdrPathObjUniquePtr, double >& rCandidate : vObjectList ) { - SdrPathObj* pObj(rCandidate.first); + SdrPathObj* pObj(rCandidate.first.get()); if(pObj->IsLine()) { @@ -2934,30 +2926,23 @@ SdrObject* EnhancedCustomShape2d::CreatePathObj( bool bLineGeometryNeededOnly ) // for some strange objects if(bSortFilledObjectsToBack) { - std::vector< std::pair< SdrPathObj*, double> > vTempList; + std::vector< std::pair< SdrPathObjUniquePtr, double> > vTempList; vTempList.reserve(vObjectList.size()); - for ( const std::pair< SdrPathObj*, double >& rCandidate : vObjectList ) + for ( std::pair< SdrPathObjUniquePtr, double >& rCandidate : vObjectList ) { - SdrPathObj* pObj(rCandidate.first); - + SdrPathObj* pObj(rCandidate.first.get()); if ( !pObj->IsLine() ) - { - vTempList.push_back(rCandidate); - } + vTempList.push_back(std::move(rCandidate)); } - for ( const std::pair< SdrPathObj*, double >& rCandidate : vObjectList ) + for ( std::pair< SdrPathObjUniquePtr, double >& rCandidate : vObjectList ) { - SdrPathObj* pObj(rCandidate.first); - - if ( pObj->IsLine() ) - { - vTempList.push_back(rCandidate); - } + if ( rCandidate.first ) + vTempList.push_back(std::move(rCandidate)); } - vObjectList = vTempList; + vObjectList = std::move(vTempList); } } } @@ -2968,18 +2953,16 @@ SdrObject* EnhancedCustomShape2d::CreatePathObj( bool bLineGeometryNeededOnly ) // copy remaining objects to pRet if(vObjectList.size() > 1) { - pRet = new SdrObjGroup(mrSdrObjCustomShape.getSdrModelFromSdrObject()); + pRet.reset(new SdrObjGroup(mrSdrObjCustomShape.getSdrModelFromSdrObject())); - for ( const std::pair< SdrPathObj*, double >& rCandidate : vObjectList ) + for ( std::pair< SdrPathObjUniquePtr, double >& rCandidate : vObjectList ) { - SdrPathObj* pObj(rCandidate.first); - - pRet->GetSubList()->NbcInsertObject(pObj); + pRet->GetSubList()->NbcInsertObject(rCandidate.first.release()); } } else if(1 == vObjectList.size()) { - pRet = vObjectList.begin()->first; + pRet.reset(vObjectList.begin()->first.release()); } if(pRet) @@ -2994,13 +2977,13 @@ SdrObject* EnhancedCustomShape2d::CreatePathObj( bool bLineGeometryNeededOnly ) return pRet; } -SdrObject* EnhancedCustomShape2d::CreateObject( bool bLineGeometryNeededOnly ) +SdrObjectUniquePtr EnhancedCustomShape2d::CreateObject( bool bLineGeometryNeededOnly ) { - SdrObject* pRet = nullptr; + SdrObjectUniquePtr pRet; if ( eSpType == mso_sptRectangle ) { - pRet = new SdrRectObj(mrSdrObjCustomShape.getSdrModelFromSdrObject(), aLogicRect); + pRet.reset(new SdrRectObj(mrSdrObjCustomShape.getSdrModelFromSdrObject(), aLogicRect)); pRet->SetMergedItemSet( *this ); } if ( !pRet ) @@ -3028,7 +3011,7 @@ void EnhancedCustomShape2d::ApplyGluePoints( SdrObject* pObj ) } } -SdrObject* EnhancedCustomShape2d::CreateLineGeometry() +SdrObjectUniquePtr EnhancedCustomShape2d::CreateLineGeometry() { return CreateObject( true ); } diff --git a/svx/source/customshapes/EnhancedCustomShapeEngine.cxx b/svx/source/customshapes/EnhancedCustomShapeEngine.cxx index d4635c4e0ddd..8e12ad83672d 100644 --- a/svx/source/customshapes/EnhancedCustomShapeEngine.cxx +++ b/svx/source/customshapes/EnhancedCustomShapeEngine.cxx @@ -402,7 +402,7 @@ drawing::PolyPolygonBezierCoords SAL_CALL EnhancedCustomShapeEngine::getLineGeom static_cast< SdrObjCustomShape& >( *GetSdrObjectFromXShape(mxShape))); EnhancedCustomShape2d aCustomShape2d(rSdrObjCustomShape); - SdrObject* pObj = aCustomShape2d.CreateLineGeometry(); + SdrObjectUniquePtr pObj = aCustomShape2d.CreateLineGeometry(); if ( pObj ) { @@ -461,7 +461,7 @@ drawing::PolyPolygonBezierCoords SAL_CALL EnhancedCustomShapeEngine::getLineGeom if ( aPP.count() ) aPolyPolygon.append(aPP); } - SdrObject::Free( pObj ); + pObj.reset(); basegfx::utils::B2DPolyPolygonToUnoPolyPolygonBezierCoords( aPolyPolygon, aPolyPolygonBezierCoords ); } -- cgit