# HG changeset patch # User Jakob Leben # Date 1397290025 25200 # Node ID 8b614632568c353c01d2a7e5c48ee9bb98a4ecea # Parent e93c6ae12526f8a2a7d1a6bf42a6d1ff6ed48e50 SingleColourLayer: fix colour reference counting * Always unref/ref colour before/after changing. * Ref colour in constructor and unref in destructor. diff -r e93c6ae12526 -r 8b614632568c layer/SingleColourLayer.cpp --- 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]--; + } +} diff -r e93c6ae12526 -r 8b614632568c layer/SingleColourLayer.h --- 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