forked from quickshell/quickshell
		
	core/scriptmodel: detatch mValues when accessed during update
Fixes iterator invalidation caused by the QML engine.
This commit is contained in:
		
							parent
							
								
									3a97da0029
								
							
						
					
					
						commit
						ed528268e0
					
				
					 2 changed files with 11 additions and 1 deletions
				
			
		| 
						 | 
					@ -12,6 +12,7 @@
 | 
				
			||||||
#include <qvariant.h>
 | 
					#include <qvariant.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void ScriptModel::updateValuesUnique(const QVariantList& newValues) {
 | 
					void ScriptModel::updateValuesUnique(const QVariantList& newValues) {
 | 
				
			||||||
 | 
						this->hasActiveIterators = true;
 | 
				
			||||||
	this->mValues.reserve(newValues.size());
 | 
						this->mValues.reserve(newValues.size());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	auto iter = this->mValues.begin();
 | 
						auto iter = this->mValues.begin();
 | 
				
			||||||
| 
						 | 
					@ -112,6 +113,8 @@ void ScriptModel::updateValuesUnique(const QVariantList& newValues) {
 | 
				
			||||||
			++newIter;
 | 
								++newIter;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						this->hasActiveIterators = false;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void ScriptModel::setValues(const QVariantList& newValues) {
 | 
					void ScriptModel::setValues(const QVariantList& newValues) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -70,7 +70,13 @@ class ScriptModel: public QAbstractListModel {
 | 
				
			||||||
	QML_ELEMENT;
 | 
						QML_ELEMENT;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
	[[nodiscard]] const QVariantList& values() const { return this->mValues; }
 | 
						[[nodiscard]] QVariantList values() const {
 | 
				
			||||||
 | 
							auto values = this->mValues;
 | 
				
			||||||
 | 
							// If not detached, the QML engine will invalidate iterators in updateValuesUnique.
 | 
				
			||||||
 | 
							if (this->hasActiveIterators) values.detach();
 | 
				
			||||||
 | 
							return values;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void setValues(const QVariantList& newValues);
 | 
						void setValues(const QVariantList& newValues);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	[[nodiscard]] qint32 rowCount(const QModelIndex& parent) const override;
 | 
						[[nodiscard]] qint32 rowCount(const QModelIndex& parent) const override;
 | 
				
			||||||
| 
						 | 
					@ -82,6 +88,7 @@ signals:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
	QVariantList mValues;
 | 
						QVariantList mValues;
 | 
				
			||||||
 | 
						bool hasActiveIterators = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void updateValuesUnique(const QVariantList& newValues);
 | 
						void updateValuesUnique(const QVariantList& newValues);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue