135360
From 565d5396ec1285508ca20544ab01569819b4b540 Mon Sep 17 00:00:00 2001
135360
From: Pranav Kant <pranavk@gnome.org>
135360
Date: Sun, 25 Oct 2015 19:22:46 +0530
135360
Subject: [PATCH 250/398] lokdocview: Fix memory leaks
135360
135360
Change-Id: I5107e4fa1828145a709e1edffe02831f4faae3c8
135360
Reviewed-on: https://gerrit.libreoffice.org/19676
135360
Tested-by: Jenkins <ci@libreoffice.org>
135360
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
135360
(cherry picked from commit cfbc36e2eade42e471056d3c32fc962cd3149c17)
135360
---
135360
 libreofficekit/source/gtk/lokdocview.cxx | 33 ++++++++++++++++----------------
135360
 libreofficekit/source/gtk/tilebuffer.cxx |  5 +++++
135360
 libreofficekit/source/gtk/tilebuffer.hxx |  8 ++++++--
135360
 3 files changed, 28 insertions(+), 18 deletions(-)
135360
135360
diff --git a/libreofficekit/source/gtk/lokdocview.cxx b/libreofficekit/source/gtk/lokdocview.cxx
135360
index 29b028c7b5d0..2ec27305af4e 100644
135360
--- a/libreofficekit/source/gtk/lokdocview.cxx
135360
+++ b/libreofficekit/source/gtk/lokdocview.cxx
135360
@@ -50,7 +50,7 @@ struct LOKDocViewPrivateImpl
135360
     LibreOfficeKit* m_pOffice;
135360
     LibreOfficeKitDocument* m_pDocument;
135360
 
135360
-    TileBuffer m_aTileBuffer;
135360
+    std::unique_ptr<TileBuffer> m_pTileBuffer;
135360
     GThreadPool* lokThreadPool;
135360
 
135360
     gfloat m_fZoom;
135360
@@ -503,9 +503,8 @@ static gboolean postDocumentLoad(gpointer pData)
135360
     // Total number of columns in this document.
135360
     guint nColumns = ceil((double)nDocumentWidthPixels / nTileSizePixels);
135360
 
135360
-
135360
-    priv->m_aTileBuffer = TileBuffer(priv->m_pDocument,
135360
-                                     nColumns);
135360
+    priv->m_pTileBuffer = std::unique_ptr<TileBuffer>(new TileBuffer(priv->m_pDocument,
135360
+                                                                     nColumns));
135360
     gtk_widget_set_size_request(GTK_WIDGET(pLOKDocView),
135360
                                 nDocumentWidthPixels,
135360
                                 nDocumentHeightPixels);
135360
@@ -634,7 +633,7 @@ setTilesInvalid (LOKDocView* pDocView, const GdkRectangle& rRectangle)
135360
         for (int j = aStart.y; j < aEnd.y; j++)
135360
         {
135360
             GTask* task = g_task_new(pDocView, NULL, NULL, NULL);
135360
-            priv->m_aTileBuffer.setInvalid(i, j, priv->m_fZoom, task, priv->lokThreadPool);
135360
+            priv->m_pTileBuffer->setInvalid(i, j, priv->m_fZoom, task, priv->lokThreadPool);
135360
             g_object_unref(task);
135360
         }
135360
     }
135360
@@ -657,7 +656,7 @@ callback (gpointer pData)
135360
             setTilesInvalid(pDocView, aRectangle);
135360
         }
135360
         else
135360
-            priv->m_aTileBuffer.resetAllTiles();
135360
+            priv->m_pTileBuffer->resetAllTiles();
135360
 
135360
         gtk_widget_queue_draw(GTK_WIDGET(pDocView));
135360
     }
135360
@@ -923,7 +922,7 @@ renderDocument(LOKDocView* pDocView, cairo_t* pCairo)
135360
             if (bPaint)
135360
             {
135360
                 GTask* task = g_task_new(pDocView, NULL, NULL, NULL);
135360
-                Tile& currentTile = priv->m_aTileBuffer.getTile(nRow, nColumn, priv->m_fZoom, task, priv->lokThreadPool);
135360
+                Tile& currentTile = priv->m_pTileBuffer->getTile(nRow, nColumn, priv->m_fZoom, task, priv->lokThreadPool);
135360
                 GdkPixbuf* pPixBuf = currentTile.getBuffer();
135360
                 gdk_cairo_set_source_pixbuf (pCairo, pPixBuf,
135360
                                              twipToPixel(aTileRectangleTwips.x, priv->m_fZoom),
135360
@@ -1484,10 +1483,10 @@ paintTileInThread (gpointer data)
135360
     LOKDocView* pDocView = LOK_DOC_VIEW(g_task_get_source_object(task));
135360
     LOKDocViewPrivate& priv = getPrivate(pDocView);
135360
     LOEvent* pLOEvent = static_cast<LOEvent*>(g_task_get_task_data(task));
135360
-    TileBuffer& buffer = priv->m_aTileBuffer;
135360
-    int index = pLOEvent->m_nPaintTileX * buffer.m_nWidth + pLOEvent->m_nPaintTileY;
135360
-    if (buffer.m_mTiles.find(index) != buffer.m_mTiles.end() &&
135360
-        buffer.m_mTiles[index].valid)
135360
+    std::unique_ptr<TileBuffer>& buffer = priv->m_pTileBuffer;
135360
+    int index = pLOEvent->m_nPaintTileX * buffer->m_nWidth + pLOEvent->m_nPaintTileY;
135360
+    if (buffer->m_mTiles.find(index) != buffer->m_mTiles.end() &&
135360
+        buffer->m_mTiles[index].valid)
135360
         return;
135360
 
135360
     GdkPixbuf* pPixBuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, nTileSizePixels, nTileSizePixels);
135360
@@ -1518,9 +1517,11 @@ paintTileInThread (gpointer data)
135360
                                          pixelToTwip(nTileSizePixels, pLOEvent->m_fPaintTileZoom));
135360
 
135360
     //create a mapping for it
135360
-    buffer.m_mTiles[index].setPixbuf(pPixBuf);
135360
-    buffer.m_mTiles[index].valid = true;
135360
+    buffer->m_mTiles[index].setPixbuf(pPixBuf);
135360
+    buffer->m_mTiles[index].valid = true;
135360
     gdk_threads_add_idle(queueDraw, GTK_WIDGET(pDocView));
135360
+
135360
+    g_object_unref(pPixBuf);
135360
 }
135360
 
135360
 
135360
@@ -2129,8 +2130,8 @@ lok_doc_view_set_zoom (LOKDocView* pDocView, float fZoom)
135360
     // Total number of columns in this document.
135360
     guint nColumns = ceil((double)nDocumentWidthPixels / nTileSizePixels);
135360
 
135360
-    priv->m_aTileBuffer = TileBuffer(priv->m_pDocument,
135360
-                                     nColumns);
135360
+    priv->m_pTileBuffer = std::unique_ptr<TileBuffer>(new TileBuffer(priv->m_pDocument,
135360
+                                                                     nColumns));
135360
     gtk_widget_set_size_request(GTK_WIDGET(pDocView),
135360
                                 nDocumentWidthPixels,
135360
                                 nDocumentHeightPixels);
135360
@@ -2211,7 +2212,7 @@ SAL_DLLPUBLIC_EXPORT void
135360
 lok_doc_view_reset_view(LOKDocView* pDocView)
135360
 {
135360
     LOKDocViewPrivate& priv = getPrivate(pDocView);
135360
-    priv->m_aTileBuffer.resetAllTiles();
135360
+    priv->m_pTileBuffer->resetAllTiles();
135360
     priv->m_nLoadProgress = 0.0;
135360
 
135360
     memset(&priv->m_aVisibleCursor, 0, sizeof(priv->m_aVisibleCursor));
135360
diff --git a/libreofficekit/source/gtk/tilebuffer.cxx b/libreofficekit/source/gtk/tilebuffer.cxx
135360
index 2f4e6cf123c1..811fcc61ef46 100644
135360
--- a/libreofficekit/source/gtk/tilebuffer.cxx
135360
+++ b/libreofficekit/source/gtk/tilebuffer.cxx
135360
@@ -41,6 +41,11 @@ void Tile::release()
135360
 
135360
 void Tile::setPixbuf(GdkPixbuf *buffer)
135360
 {
135360
+    if (m_pBuffer == buffer)
135360
+        return;
135360
+    g_clear_object(&m_pBuffer);
135360
+    if (buffer != NULL)
135360
+        g_object_ref(buffer);
135360
     m_pBuffer = buffer;
135360
 }
135360
 
135360
diff --git a/libreofficekit/source/gtk/tilebuffer.hxx b/libreofficekit/source/gtk/tilebuffer.hxx
135360
index 9361a622fb7c..bef1444f9c9c 100644
135360
--- a/libreofficekit/source/gtk/tilebuffer.hxx
135360
+++ b/libreofficekit/source/gtk/tilebuffer.hxx
135360
@@ -52,7 +52,10 @@ class Tile
135360
 {
135360
  public:
135360
     Tile() : valid(false), m_pBuffer(0) {}
135360
-    ~Tile() { }
135360
+    ~Tile()
135360
+    {
135360
+        g_clear_object(&m_pBuffer);
135360
+    }
135360
 
135360
     /**
135360
        Tells if this tile is valid or not. Initialised to 0 (invalid) during
135360
@@ -85,10 +88,11 @@ class TileBuffer
135360
  TileBuffer(LibreOfficeKitDocument *document = 0,
135360
             int columns = 0)
135360
      : m_pLOKDocument(document)
135360
-        , m_nWidth(columns)
135360
+     , m_nWidth(columns)
135360
     {
135360
         GdkPixbuf* pPixBuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, nTileSizePixels, nTileSizePixels);
135360
         m_DummyTile.setPixbuf(pPixBuf);
135360
+        g_object_unref(pPixBuf);
135360
     }
135360
 
135360
     ~TileBuffer() {}
135360
-- 
135360
2.12.0
135360