[Libreoffice-commits] core.git: vcl/source

Takeshi Abe tabe at fixedpoint.jp
Fri Mar 14 03:21:44 PDT 2014


 vcl/source/gdi/bitmap.cxx  |   53 ++++++++++--------------------
 vcl/source/gdi/bitmap3.cxx |   78 ++++++++++++++++-----------------------------
 2 files changed, 46 insertions(+), 85 deletions(-)

New commits:
commit 61f9aa6176629c2f8774f4f1ea44ca8c4d8b3c09
Author: Takeshi Abe <tabe at fixedpoint.jp>
Date:   Thu Mar 13 23:59:50 2014 +0900

    Avoid possible resource leaks by boost::scoped_array
    
    Change-Id: If9b4a70895ae22ef40d9d1fa83ecbf9131c836af

diff --git a/vcl/source/gdi/bitmap.cxx b/vcl/source/gdi/bitmap.cxx
index af11af2..2bd739e2 100644
--- a/vcl/source/gdi/bitmap.cxx
+++ b/vcl/source/gdi/bitmap.cxx
@@ -32,6 +32,7 @@
 
 #include <impbmp.hxx>
 #include <salbmp.hxx>
+#include <boost/scoped_array.hpp>
 
 Bitmap::Bitmap() :
     mpImpBmp( NULL )
@@ -584,19 +585,18 @@ bool Bitmap::Mirror( sal_uLong nMirrorFlags )
         if( pAcc )
         {
             const long  nScanSize = pAcc->GetScanlineSize();
-            sal_uInt8*      pBuffer = new sal_uInt8[ nScanSize ];
+            boost::scoped_array<sal_uInt8> pBuffer(new sal_uInt8[ nScanSize ]);
             const long  nHeight = pAcc->Height();
             const long  nHeight1 = nHeight - 1L;
             const long  nHeight_2 = nHeight >> 1L;
 
             for( long nY = 0L, nOther = nHeight1; nY < nHeight_2; nY++, nOther-- )
             {
-                memcpy( pBuffer, pAcc->GetScanline( nY ), nScanSize );
+                memcpy( pBuffer.get(), pAcc->GetScanline( nY ), nScanSize );
                 memcpy( pAcc->GetScanline( nY ), pAcc->GetScanline( nOther ), nScanSize );
-                memcpy( pAcc->GetScanline( nOther ), pBuffer, nScanSize );
+                memcpy( pAcc->GetScanline( nOther ), pBuffer.get(), nScanSize );
             }
 
-            delete[] pBuffer;
             ReleaseAccess( pAcc );
             bRet = true;
         }
@@ -726,10 +726,10 @@ bool Bitmap::Rotate( long nAngle10, const Color& rFillColor )
                     long                nRotY;
                     long                nSinY;
                     long                nCosY;
-                    long*               pCosX = new long[ nNewWidth ];
-                    long*               pSinX = new long[ nNewWidth ];
-                    long*               pCosY = new long[ nNewHeight ];
-                    long*               pSinY = new long[ nNewHeight ];
+                    boost::scoped_array<long> pCosX(new long[ nNewWidth ]);
+                    boost::scoped_array<long> pSinX(new long[ nNewWidth ]);
+                    boost::scoped_array<long> pCosY(new long[ nNewHeight ]);
+                    boost::scoped_array<long> pSinY(new long[ nNewHeight ]);
 
                     for ( nX = 0; nX < nNewWidth; nX++ )
                     {
@@ -764,11 +764,6 @@ bool Bitmap::Rotate( long nAngle10, const Color& rFillColor )
                         }
                     }
 
-                    delete[] pSinX;
-                    delete[] pCosX;
-                    delete[] pSinY;
-                    delete[] pCosY;
-
                     aNewBmp.ReleaseAccess( pWriteAcc );
                 }
 
@@ -925,7 +920,7 @@ bool Bitmap::CopyPixel( const Rectangle& rRectDst,
                         if( pReadAcc->HasPalette() && pWriteAcc->HasPalette() )
                         {
                             const sal_uInt16    nCount = pReadAcc->GetPaletteEntryCount();
-                            sal_uInt8*          pMap = new sal_uInt8[ nCount ];
+                            boost::scoped_array<sal_uInt8> pMap(new sal_uInt8[ nCount ]);
 
                             // Create index map for the color table, as the bitmap should be copied
                             // retaining it's color information relatively well
@@ -935,8 +930,6 @@ bool Bitmap::CopyPixel( const Rectangle& rRectDst,
                             for( long nSrcY = aRectSrc.Top(); nSrcY < nSrcEndY; nSrcY++, nDstY++ )
                                 for( long nSrcX = aRectSrc.Left(), nDstX = aRectDst.Left(); nSrcX < nSrcEndX; nSrcX++, nDstX++ )
                                     pWriteAcc->SetPixelIndex( nDstY, nDstX, pMap[ pReadAcc->GetPixelIndex( nSrcY, nSrcX ) ] );
-
-                            delete[] pMap;
                         }
                         else if( pReadAcc->HasPalette() )
                         {
@@ -1403,10 +1396,10 @@ bool Bitmap::Replace( const Bitmap& rMask, const Color& rReplaceColor )
                 }
                 else
                 {
-                    bool* pFlags = new bool[ nMaxColors ];
+                    boost::scoped_array<bool> pFlags(new bool[ nMaxColors ]);
 
                     // Set all entries to false
-                    std::fill( pFlags, pFlags+nMaxColors, false );
+                    std::fill( pFlags.get(), pFlags.get()+nMaxColors, false );
 
                     for( long nY = 0L; nY < nHeight; nY++ )
                         for( long nX = 0L; nX < nWidth; nX++ )
@@ -1421,8 +1414,6 @@ bool Bitmap::Replace( const Bitmap& rMask, const Color& rReplaceColor )
                             aReplace = BitmapColor( (sal_uInt8) i );
                         }
                     }
-
-                    delete[] pFlags;
                 }
             }
         }
@@ -1561,12 +1552,12 @@ bool Bitmap::Replace( const Color* pSearchColors, const Color* pReplaceColors,
 
     if( pAcc )
     {
-        long*   pMinR = new long[ nColorCount ];
-        long*   pMaxR = new long[ nColorCount ];
-        long*   pMinG = new long[ nColorCount ];
-        long*   pMaxG = new long[ nColorCount ];
-        long*   pMinB = new long[ nColorCount ];
-        long*   pMaxB = new long[ nColorCount ];
+        boost::scoped_array<long> pMinR(new long[ nColorCount ]);
+        boost::scoped_array<long> pMaxR(new long[ nColorCount ]);
+        boost::scoped_array<long> pMinG(new long[ nColorCount ]);
+        boost::scoped_array<long> pMaxG(new long[ nColorCount ]);
+        boost::scoped_array<long> pMinB(new long[ nColorCount ]);
+        boost::scoped_array<long> pMaxB(new long[ nColorCount ]);
         long*   pTols;
         sal_uLong   i;
 
@@ -1612,7 +1603,7 @@ bool Bitmap::Replace( const Color* pSearchColors, const Color* pReplaceColors,
         else
         {
             BitmapColor     aCol;
-            BitmapColor*    pReplaces = new BitmapColor[ nColorCount ];
+            boost::scoped_array<BitmapColor> pReplaces(new BitmapColor[ nColorCount ]);
 
             for( i = 0UL; i < nColorCount; i++ )
                 pReplaces[ i ] = pAcc->GetBestMatchingColor( pReplaceColors[ i ] );
@@ -1635,19 +1626,11 @@ bool Bitmap::Replace( const Color* pSearchColors, const Color* pReplaceColors,
                     }
                 }
             }
-
-            delete[] pReplaces;
         }
 
         if( !_pTols )
             delete[] pTols;
 
-        delete[] pMinR;
-        delete[] pMaxR;
-        delete[] pMinG;
-        delete[] pMaxG;
-        delete[] pMinB;
-        delete[] pMaxB;
         ReleaseAccess( pAcc );
         bRet = true;
     }
diff --git a/vcl/source/gdi/bitmap3.cxx b/vcl/source/gdi/bitmap3.cxx
index 6ee351f..f8bdb4e 100644
--- a/vcl/source/gdi/bitmap3.cxx
+++ b/vcl/source/gdi/bitmap3.cxx
@@ -680,9 +680,9 @@ bool Bitmap::ImplConvertDown( sal_uInt16 nBitCount, Color* pExtColor )
             InverseColorMap aColorMap( aPal = aOctree.GetPalette() );
             BitmapColor aColor;
             ImpErrorQuad aErrQuad;
-            ImpErrorQuad* pErrQuad1 = new ImpErrorQuad[ nWidth ];
-            ImpErrorQuad* pErrQuad2 = new ImpErrorQuad[ nWidth ];
-            ImpErrorQuad* pQLine1 = pErrQuad1;
+            boost::scoped_array<ImpErrorQuad> pErrQuad1(new ImpErrorQuad[ nWidth ]);
+            boost::scoped_array<ImpErrorQuad> pErrQuad2(new ImpErrorQuad[ nWidth ]);
+            ImpErrorQuad* pQLine1 = pErrQuad1.get();
             ImpErrorQuad* pQLine2 = 0;
             long nX, nY;
             long nYTmp = 0L;
@@ -707,7 +707,7 @@ bool Bitmap::ImplConvertDown( sal_uInt16 nBitCount, Color* pExtColor )
 
             for( nY = 0L; nY < std::min( nHeight, 2L ); nY++, nYTmp++ )
             {
-                for( nX = 0L, pQLine2 = !nY ? pErrQuad1 : pErrQuad2; nX < nWidth; nX++ )
+                for( nX = 0L, pQLine2 = !nY ? pErrQuad1.get() : pErrQuad2.get(); nX < nWidth; nX++ )
                 {
                     if( pReadAcc->HasPalette() )
                         pQLine2[ nX ] = pReadAcc->GetPaletteColor( pReadAcc->GetPixelIndex( nYTmp, nX ) );
@@ -742,7 +742,7 @@ bool Bitmap::ImplConvertDown( sal_uInt16 nBitCount, Color* pExtColor )
 
                 // Refill/copy row buffer
                 pQLine1 = pQLine2;
-                pQLine2 = ( bQ1 = !bQ1 ) ? pErrQuad2 : pErrQuad1;
+                pQLine2 = ( bQ1 = !bQ1 ) ? pErrQuad2.get() : pErrQuad1.get();
 
                 if( nYTmp < nHeight )
                 {
@@ -756,10 +756,6 @@ bool Bitmap::ImplConvertDown( sal_uInt16 nBitCount, Color* pExtColor )
                 }
             }
 
-            // Delete row buffer
-            delete[] pErrQuad1;
-            delete[] pErrQuad2;
-
             aNewBmp.ReleaseAccess( pWriteAcc );
             bRet = true;
         }
@@ -1047,8 +1043,8 @@ bool Bitmap::ImplScaleFast( const double& rScaleX, const double& rScaleY )
                 const long nNewHeight1 = nNewHeight - 1L;
                 const long nWidth = pReadAcc->Width();
                 const long nHeight = pReadAcc->Height();
-                long* pLutX = new long[ nNewWidth ];
-                long* pLutY = new long[ nNewHeight ];
+                boost::scoped_array<long> pLutX(new long[ nNewWidth ]);
+                boost::scoped_array<long> pLutY(new long[ nNewHeight ]);
 
                 if( nNewWidth1 && nNewHeight1 )
                 {
@@ -1078,9 +1074,6 @@ bool Bitmap::ImplScaleFast( const double& rScaleX, const double& rScaleY )
                     bRet = true;
                     aNewBmp.ReleaseAccess( pWriteAcc );
                 }
-
-                delete[] pLutX;
-                delete[] pLutY;
             }
             ReleaseAccess( pReadAcc );
 
@@ -1106,8 +1099,6 @@ bool Bitmap::ImplScaleInterpolate( const double& rScaleX, const double& rScaleY
         long nHeight = pReadAcc->Height();
         Bitmap aNewBmp( Size( nNewWidth, nHeight ), 24 );
         BitmapWriteAccess* pWriteAcc = aNewBmp.AcquireWriteAccess();
-        long* pLutInt;
-        long* pLutFrac;
         long nX, nY;
         long lXB0, lXB1, lXG0, lXG1, lXR0, lXR1;
         double fTemp;
@@ -1119,8 +1110,8 @@ bool Bitmap::ImplScaleInterpolate( const double& rScaleX, const double& rScaleY
             const long nWidth1 = pReadAcc->Width() - 1L;
             const double fRevScaleX = (double) nWidth1 / nNewWidth1;
 
-            pLutInt = new long[ nNewWidth ];
-            pLutFrac = new long[ nNewWidth ];
+            boost::scoped_array<long> pLutInt(new long[ nNewWidth ]);
+            boost::scoped_array<long> pLutFrac(new long[ nNewWidth ]);
 
             for( nX = 0L, nTemp = nWidth - 2L; nX < nNewWidth; nX++ )
             {
@@ -1182,8 +1173,6 @@ bool Bitmap::ImplScaleInterpolate( const double& rScaleX, const double& rScaleY
                 }
             }
 
-            delete[] pLutInt;
-            delete[] pLutFrac;
             bRet = true;
         }
 
@@ -1205,8 +1194,8 @@ bool Bitmap::ImplScaleInterpolate( const double& rScaleX, const double& rScaleY
                 const long nHeight1 = pReadAcc->Height() - 1L;
                 const double fRevScaleY = (double) nHeight1 / nNewHeight1;
 
-                pLutInt = new long[ nNewHeight ];
-                pLutFrac = new long[ nNewHeight ];
+                boost::scoped_array<long> pLutInt(new long[ nNewHeight ]);
+                boost::scoped_array<long> pLutFrac(new long[ nNewHeight ]);
 
                 for( nY = 0L, nTemp = nHeight - 2L; nY < nNewHeight; nY++ )
                 {
@@ -1254,8 +1243,6 @@ bool Bitmap::ImplScaleInterpolate( const double& rScaleX, const double& rScaleY
                     }
                 }
 
-                delete[] pLutInt;
-                delete[] pLutFrac;
                 bRet = true;
             }
 
@@ -2551,10 +2538,10 @@ bool Bitmap::ImplDitherFloyd()
             long nW2 = nW - 3L;
             long nRErr, nGErr, nBErr;
             long nRC, nGC, nBC;
-            long* p1 = new long[ nW ];
-            long* p2 = new long[ nW ];
-            long* p1T = p1;
-            long* p2T = p2;
+            boost::scoped_array<long> p1(new long[ nW ]);
+            boost::scoped_array<long> p2(new long[ nW ]);
+            long* p1T = p1.get();
+            long* p2T = p2.get();
             long* pTmp;
             bool bPal = pReadAcc->HasPalette();
 
@@ -2644,8 +2631,6 @@ bool Bitmap::ImplDitherFloyd()
                 pWriteAcc->SetPixelIndex( nYAcc, nWidth1, static_cast<sal_uInt8>(nVCLBLut[ nBC ] + nVCLGLut[nGC ] + nVCLRLut[nRC ]) );
             }
 
-            delete[] p1;
-            delete[] p2;
             bRet = true;
         }
 
@@ -2682,16 +2667,16 @@ bool Bitmap::ImplDitherFloyd16()
         BitmapColor aColor;
         BitmapColor aBestCol;
         ImpErrorQuad aErrQuad;
-        ImpErrorQuad* pErrQuad1 = new ImpErrorQuad[ nWidth ];
-        ImpErrorQuad* pErrQuad2 = new ImpErrorQuad[ nWidth ];
-        ImpErrorQuad* pQLine1 = pErrQuad1;
+        boost::scoped_array<ImpErrorQuad> pErrQuad1(new ImpErrorQuad[ nWidth ]);
+        boost::scoped_array<ImpErrorQuad> pErrQuad2(new ImpErrorQuad[ nWidth ]);
+        ImpErrorQuad* pQLine1 = pErrQuad1.get();
         ImpErrorQuad* pQLine2 = 0;
         long nX, nY;
         long nYTmp = 0L;
         bool bQ1 = true;
 
         for( nY = 0L; nY < std::min( nHeight, 2L ); nY++, nYTmp++ )
-            for( nX = 0L, pQLine2 = !nY ? pErrQuad1 : pErrQuad2; nX < nWidth; nX++ )
+            for( nX = 0L, pQLine2 = !nY ? pErrQuad1.get() : pErrQuad2.get(); nX < nWidth; nX++ )
                 pQLine2[ nX ] = pReadAcc->GetPixel( nYTmp, nX );
 
         for( nY = 0L; nY < nHeight; nY++, nYTmp++ )
@@ -2726,16 +2711,13 @@ bool Bitmap::ImplDitherFloyd16()
 
             // Refill/copy row buffer
             pQLine1 = pQLine2;
-            pQLine2 = ( bQ1 = !bQ1 ) ? pErrQuad2 : pErrQuad1;
+            pQLine2 = ( bQ1 = !bQ1 ) ? pErrQuad2.get() : pErrQuad1.get();
 
             if( nYTmp < nHeight )
                 for( nX = 0L; nX < nWidth; nX++ )
                     pQLine2[ nX ] = pReadAcc->GetPixel( nYTmp, nX );
         }
 
-        // Destroy row buffer
-        delete[] pErrQuad1;
-        delete[] pErrQuad2;
         bRet = true;
     }
 
@@ -2884,10 +2866,10 @@ bool Bitmap::ImplReducePopular( sal_uInt16 nColCount )
         const sal_uInt32 nTotalColors = nColorsPerComponent * nColorsPerComponent * nColorsPerComponent;
         const long nWidth = pRAcc->Width();
         const long nHeight = pRAcc->Height();
-        PopularColorCount* pCountTable = new PopularColorCount[ nTotalColors ];
+        boost::scoped_array<PopularColorCount> pCountTable(new PopularColorCount[ nTotalColors ]);
         long nX, nY, nR, nG, nB, nIndex;
 
-        memset( pCountTable, 0, nTotalColors * sizeof( PopularColorCount ) );
+        memset( pCountTable.get(), 0, nTotalColors * sizeof( PopularColorCount ) );
 
         for( nR = 0, nIndex = 0; nR < 256; nR += nColorOffset )
         {
@@ -2930,7 +2912,7 @@ bool Bitmap::ImplReducePopular( sal_uInt16 nColCount )
 
         BitmapPalette aNewPal( nColCount );
 
-        qsort( pCountTable, nTotalColors, sizeof( PopularColorCount ), ImplPopularCmpFnc );
+        qsort( pCountTable.get(), nTotalColors, sizeof( PopularColorCount ), ImplPopularCmpFnc );
 
         for( sal_uInt16 n = 0; n < nColCount; n++ )
         {
@@ -2946,7 +2928,7 @@ bool Bitmap::ImplReducePopular( sal_uInt16 nColCount )
         if( pWAcc )
         {
             BitmapColor aDstCol( (sal_uInt8) 0 );
-            sal_uInt8* pIndexMap = new sal_uInt8[ nTotalColors ];
+            boost::scoped_array<sal_uInt8> pIndexMap(new sal_uInt8[ nTotalColors ]);
 
             for( nR = 0, nIndex = 0; nR < 256; nR += nColorOffset )
                 for( nG = 0; nG < 256; nG += nColorOffset )
@@ -2982,12 +2964,11 @@ bool Bitmap::ImplReducePopular( sal_uInt16 nColCount )
                 }
             }
 
-            delete[] pIndexMap;
             aNewBmp.ReleaseAccess( pWAcc );
             bRet = true;
         }
 
-        delete[] pCountTable;
+        pCountTable.reset();
         ReleaseAccess( pRAcc );
 
         if( bRet )
@@ -3256,9 +3237,9 @@ bool Bitmap::Adjust( short nLuminancePercent, short nContrastPercent,
             BitmapColor aCol;
             const long nW = pAcc->Width();
             const long nH = pAcc->Height();
-            sal_uInt8* cMapR = new sal_uInt8[ 256 ];
-            sal_uInt8* cMapG = new sal_uInt8[ 256 ];
-            sal_uInt8* cMapB = new sal_uInt8[ 256 ];
+            boost::scoped_array<sal_uInt8> cMapR(new sal_uInt8[ 256 ]);
+            boost::scoped_array<sal_uInt8> cMapG(new sal_uInt8[ 256 ]);
+            boost::scoped_array<sal_uInt8> cMapB(new sal_uInt8[ 256 ]);
             long nX, nY;
             double fM, fROff, fGOff, fBOff, fOff;
 
@@ -3359,9 +3340,6 @@ bool Bitmap::Adjust( short nLuminancePercent, short nContrastPercent,
                 }
             }
 
-            delete[] cMapR;
-            delete[] cMapG;
-            delete[] cMapB;
             ReleaseAccess( pAcc );
             bRet = true;
         }


More information about the Libreoffice-commits mailing list