When does QImage’s detach method perform a deep copy of the data?
-
uchar buf[1000] = {}; QImage img(buf,10,10,QImage::Format_Grayscale8); auto d1 = img.constBits(); img.detach(); auto d2 = img.constBits(); img = img.copy(); auto d3 = img.constBits(); img.detach(); auto d4 = img.constBits();
"detach" has never been effective.
-
When does QImage’s detach method perform a deep copy of the data?
What QImage::detach() method? Cannot see this documented anywhere (Qt 5, Qt 6).
There is an internal buffer that is detached (deep copied) from other, shared copies of that buffer when one of the QImage instances calls a non-const method on itself. If there is no sharing of a buffer occurring, then there is no deep copy to be done. If there is only one QImage then there cannot be any sharing of a buffer.
-
uchar buf[1000] = {}; QImage img(buf,10,10,QImage::Format_Grayscale8); auto d1 = img.constBits(); img.detach(); auto d2 = img.constBits(); img = img.copy(); auto d3 = img.constBits(); img.detach(); auto d4 = img.constBits();
"detach" has never been effective.
@John-Van said in When does QImage’s detach method perform a deep copy of the data?:
"detach" has never been effective.
Why should it? The reference count is never > 1 so no detach needed.
See https://doc.qt.io/qt-6/implicit-sharing.html -
When does QImage’s detach method perform a deep copy of the data?
What QImage::detach() method? Cannot see this documented anywhere (Qt 5, Qt 6).
There is an internal buffer that is detached (deep copied) from other, shared copies of that buffer when one of the QImage instances calls a non-const method on itself. If there is no sharing of a buffer occurring, then there is no deep copy to be done. If there is only one QImage then there cannot be any sharing of a buffer.
-
When does QImage’s detach method perform a deep copy of the data?
What QImage::detach() method? Cannot see this documented anywhere (Qt 5, Qt 6).
There is an internal buffer that is detached (deep copied) from other, shared copies of that buffer when one of the QImage instances calls a non-const method on itself. If there is no sharing of a buffer occurring, then there is no deep copy to be done. If there is only one QImage then there cannot be any sharing of a buffer.
@ChrisW67 Well @John-Van calls constBits. But by looking into the documentation it is clear that constBits does not detach:
"Note that QImage uses implicit data sharing, but this function does not perform a deep copy of the shared pixel data, because the returned data is const."
https://doc.qt.io/qt-6/qimage.html#constBits -