@@ -643,6 +643,27 @@ bool QgsSymbol::hasDataDefinedProperties() const
643
643
return false ;
644
644
}
645
645
646
+ // /@cond PRIVATE
647
+
648
+ /* *
649
+ * RAII class to pop scope from an expression context on destruction
650
+ */
651
+ class ExpressionContextScopePopper
652
+ {
653
+ public:
654
+
655
+ ExpressionContextScopePopper () = default ;
656
+
657
+ ~ExpressionContextScopePopper ()
658
+ {
659
+ if ( context )
660
+ context->popScope ();
661
+ }
662
+
663
+ QgsExpressionContext *context = nullptr ;
664
+ };
665
+ // /@endcond PRIVATE
666
+
646
667
void QgsSymbol::renderFeature ( const QgsFeature &feature, QgsRenderContext &context, int layer, bool selected, bool drawVertexMarker, int currentVertexMarkerType, int currentVertexMarkerSize )
647
668
{
648
669
QgsGeometry geom = feature.geometry ();
@@ -672,9 +693,17 @@ void QgsSymbol::renderFeature( const QgsFeature &feature, QgsRenderContext &cont
672
693
mSymbolRenderContext ->setGeometryPartCount ( segmentizedGeometry.geometry ()->partCount () );
673
694
mSymbolRenderContext ->setGeometryPartNum ( 1 );
674
695
696
+ ExpressionContextScopePopper scopePopper;
675
697
if ( mSymbolRenderContext ->expressionContextScope () )
676
698
{
699
+ // this is somewhat nasty - by appending this scope here it's now owned
700
+ // by both mSymbolRenderContext AND context.expressionContext()
701
+ // the RAII scopePopper is required to make sure it always has ownership transferred back
702
+ // from context.expressionContext(), even if exceptions of other early exits occur in this
703
+ // function
677
704
context.expressionContext ().appendScope ( mSymbolRenderContext ->expressionContextScope () );
705
+ scopePopper.context = &context.expressionContext ();
706
+
678
707
QgsExpressionContextUtils::updateSymbolScope ( this , mSymbolRenderContext ->expressionContextScope () );
679
708
mSymbolRenderContext ->expressionContextScope ()->addVariable ( QgsExpressionContextScope::StaticVariable ( QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT, mSymbolRenderContext ->geometryPartCount (), true ) );
680
709
mSymbolRenderContext ->expressionContextScope ()->addVariable ( QgsExpressionContextScope::StaticVariable ( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, 1 , true ) );
@@ -952,9 +981,6 @@ void QgsSymbol::renderFeature( const QgsFeature &feature, QgsRenderContext &cont
952
981
}
953
982
}
954
983
}
955
-
956
- if ( mSymbolRenderContext ->expressionContextScope () )
957
- context.expressionContext ().popScope ();
958
984
}
959
985
960
986
QgsSymbolRenderContext *QgsSymbol::symbolRenderContext ()
0 commit comments