changeset 768:8b614632568c

SingleColourLayer: fix colour reference counting * Always unref/ref colour before/after changing. * Ref colour in constructor and unref in destructor.
author Jakob Leben <jakob.leben@gmail.com>
date Sat, 12 Apr 2014 01:07:05 -0700
parents e93c6ae12526
children 734ee80286c3 a964151832a7
files layer/SingleColourLayer.cpp layer/SingleColourLayer.h
diffstat 2 files changed, 37 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/layer/SingleColourLayer.cpp	Fri Apr 11 23:22:55 2014 -0700
+++ b/layer/SingleColourLayer.cpp	Sat Apr 12 01:07:05 2014 -0700
@@ -32,9 +32,17 @@
     m_colourExplicitlySet(false),
     m_defaultColourSet(false)
 {
+    // Reference current colour because setDefaulColourFor
+    // will unreference it before (possibly) changing it.
+    refColor();
     setDefaultColourFor(0);
 }
 
+SingleColourLayer::~SingleColourLayer()
+{
+    unrefColor();
+}
+
 QPixmap
 SingleColourLayer::getLayerPresentationPixmap(QSize size) const
 {
@@ -152,10 +160,6 @@
     int hint = -1;
     bool impose = false;
     if (v) {
-        if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() &&
-            m_colourRefCount[m_colour] > 0) {
-            m_colourRefCount[m_colour]--;
-        }
         // We don't want to call this if !v because that probably
         // means we're being called from the constructor, and this is
         // a virtual function
@@ -174,6 +178,8 @@
         return;
     }
 
+    unrefColor();
+
     int bestCount = 0, bestColour = -1;
     
     for (int i = 0; i < cdb->getColourCount(); ++i) {
@@ -203,15 +209,11 @@
         cerr << endl;
 #endif
     }
-    
+
     if (bestColour < 0) m_colour = 0;
     else m_colour = bestColour;
 
-    if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
-        m_colourRefCount[m_colour] = 1;
-    } else {
-        m_colourRefCount[m_colour]++;
-    }
+    refColor();
 }
 
 void
@@ -221,18 +223,9 @@
 
     if (m_colour == colour) return;
 
-    if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() &&
-        m_colourRefCount[m_colour] > 0) {
-        m_colourRefCount[m_colour]--;
-    }
-
+    refColor();
     m_colour = colour;
-
-    if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
-        m_colourRefCount[m_colour] = 1;
-    } else {
-        m_colourRefCount[m_colour]++;
-    }
+    unrefColor();
 
     flagBaseColourChanged();
     emit layerParametersChanged();
@@ -318,20 +311,27 @@
         SVDEBUG << "SingleColourLayer::setProperties: changing colour from " << m_colour << " to " << colour << endl;
 #endif
 
-        if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() &&
-            m_colourRefCount[m_colour] > 0) {
-            m_colourRefCount[m_colour]--;
-        }
-
+        unrefColor();
         m_colour = colour;
-
-        if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
-            m_colourRefCount[m_colour] = 1;
-        } else {
-            m_colourRefCount[m_colour]++;
-        }
+        refColor();
 
         flagBaseColourChanged();
     }
 }
 
+void SingleColourLayer::refColor()
+{
+    if (m_colourRefCount.find(m_colour) == m_colourRefCount.end()) {
+        m_colourRefCount[m_colour] = 1;
+    } else {
+        m_colourRefCount[m_colour]++;
+    }
+}
+
+void SingleColourLayer::unrefColor()
+{
+    if (m_colourRefCount.find(m_colour) != m_colourRefCount.end() &&
+        m_colourRefCount[m_colour] > 0) {
+        m_colourRefCount[m_colour]--;
+    }
+}
--- a/layer/SingleColourLayer.h	Fri Apr 11 23:22:55 2014 -0700
+++ b/layer/SingleColourLayer.h	Sat Apr 12 01:07:05 2014 -0700
@@ -75,6 +75,7 @@
 
 protected:
     SingleColourLayer();
+    virtual ~SingleColourLayer();
 
     virtual QColor getBaseQColor() const;
     virtual QColor getBackgroundQColor(View *v) const;
@@ -91,6 +92,10 @@
     int m_colour;
     bool m_colourExplicitlySet;
     bool m_defaultColourSet;
+
+private:
+    void refColor();
+    void unrefColor();
 };
 
 #endif