Solved sharing/copying a Qbytearray
-
Hello every one .
I receive some data over a socket and store it in a local QBytearray (located on the stack).QBytearray rxMsg;
The rcvd data will pass over a few functions while being processed. I want to avoid un-necessary copying in this process.
1- I think passing this data as a refernece would cause me problemsvoid onProcessMsg(QBytearray& data)
(because the rxMsg is on the local stack of the first function that would be destroyed upon returning). Is my understanding right?
2- should I use smart pointers or using QBytearray itself would suffice (The Qbytearray documentation mentions about "implicit sharing"; which i dont fully grasp)
Thanks in advance. -
Hi
http://doc.qt.io/qt-5/implicit-sharing.htmlAnyway, if the functions calls are in same scope then its ok with reference.
void main_func() {
QBytearray rxMsg;
func_call(rxMsg)
func_call2(rxMsg)} // rxMsg killed by scope
but why not just make it member of the class ? then it lives as long as class.
-
Hi
http://doc.qt.io/qt-5/implicit-sharing.htmlAnyway, if the functions calls are in same scope then its ok with reference.
void main_func() {
QBytearray rxMsg;
func_call(rxMsg)
func_call2(rxMsg)} // rxMsg killed by scope
but why not just make it member of the class ? then it lives as long as class.
-
Hi,
As mrjj said, make that as member variable of a class so that there will be no confusion around the data.
-
@mrjj thanks a lot (the link was also very usefull)