Qt Implicit sharing


STL style iterators on Qt Container can have some negative side effect due to the implicit-sharing. It is advised to avoid copying a Qt container while you have iterators active on them.

QVector<int> a,b; //2 vectors
b = a; // b and a now point to the same memory internally

auto iter = a.begin(); //iter also points to the same memory a and b do
a[4] = 1; //a creates a new copy and points to different memory.
//Warning 1: b and iter point sill to the same even if iter was "a.begin()"

b.clear(); //delete b-memory
//Warning 2: iter only holds a pointer to the memory but does not increase ref-count. 
//           so now the memory iter points to is invalid. UB!