No, its asking for trouble. It is like a function returing a pointer to local data - after singal is emitted, local data will be destroyed during the stack unwinding. If you pass a reference (/ pointer) to that data outside of this function, via deferred signal or by return statement, you are giving someone a handle to non-existing object. And I'm pretty sure you know how bad this is 
void Class::badIdea(){
BigData data = ...;
emit dataOut(&data); // if connection is queued you are in trouble
} // data is DEADBEEF now :) and queued signal is maybe still waiting in the queue...
void Class::badIdea(){
BigData data = ...;
emit dataOut(&data); // if connection is queued you are in trouble
} // data is DEADBEEF now :) and queued signal is maybe still waiting in the queue...
To copy to clipboard, switch view to plain text mode
If you are afraid of the cost of copy operation, use resource handles like containers (which are implicitly shared) or use shared pointers explicitly.
You can also just use heap-allocated object and emit it directly, but I'd rather avoid the headaches related to ownership rules:
void Class::func(){
// we dont want to copy the BigData, so we are "smart" - we use heap allocation and simply send a pointer
BigData * data = new BigData();
emit dataOut(data);
// questions :
// 1. who should delete the data ?
// 2. what if no one is connected ?
}
void Class::func(){
// we dont want to copy the BigData, so we are "smart" - we use heap allocation and simply send a pointer
BigData * data = new BigData();
emit dataOut(data);
// questions :
// 1. who should delete the data ?
// 2. what if no one is connected ?
}
To copy to clipboard, switch view to plain text mode
and with shared pointers:
typedef QSharedPointer<BigData> BigDataPtr;
void Class::func(){
BigDataPtr data(new BigData);
emit dataOut(data);
// answers for free:
// 1. shared data will be deleted when underlying shared ptr's reference counter drops to zero
// 2. no problem, data is deleted automatically as the only shared pointer referencing the data will be gone soon
}
typedef QSharedPointer<BigData> BigDataPtr;
void Class::func(){
BigDataPtr data(new BigData);
emit dataOut(data);
// answers for free:
// 1. shared data will be deleted when underlying shared ptr's reference counter drops to zero
// 2. no problem, data is deleted automatically as the only shared pointer referencing the data will be gone soon
}
To copy to clipboard, switch view to plain text mode
Bookmarks