Results 1 to 6 of 6

Thread: Safety of emitting a signal with a reference

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Safety of emitting a signal with a reference

    Hello!

    Suppose I want to emit a signal that sends not some data, but the reference of that data (less memory consumption). Would that be safe if the variable referenced to is local to the method and the connection is not a Qt:irectConnection? Or even in this situation that wouldn't be safe, so one should always use non-reference parameter in his signals?

    Thanks,

    Momergil
    May the Lord be with you. Always.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Safety of emitting a signal with a reference

    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
    Qt Code:
    1. void Class::badIdea(){
    2. BigData data = ...;
    3. emit dataOut(&data); // if connection is queued you are in trouble
    4. } // 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:
    Qt Code:
    1. void Class::func(){
    2. // we dont want to copy the BigData, so we are "smart" - we use heap allocation and simply send a pointer
    3. BigData * data = new BigData();
    4. emit dataOut(data);
    5. // questions :
    6. // 1. who should delete the data ?
    7. // 2. what if no one is connected ?
    8. }
    To copy to clipboard, switch view to plain text mode 
    and with shared pointers:
    Qt Code:
    1. typedef QSharedPointer<BigData> BigDataPtr;
    2.  
    3. void Class::func(){
    4. BigDataPtr data(new BigData);
    5. emit dataOut(data);
    6. // answers for free:
    7. // 1. shared data will be deleted when underlying shared ptr's reference counter drops to zero
    8. // 2. no problem, data is deleted automatically as the only shared pointer referencing the data will be gone soon
    9. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to stampede for this useful post:

    Momergil (4th July 2014)

  4. #3
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Safety of emitting a signal with a reference

    Thanks stampede for your reply!

    One last question that still isn't clear enough: you sad in your first "badIdea()" code:

    if connection is queued
    so actually such problems only exists in non-direct connections, right? Which means that if the situation allows me, another way of going around this problem is making my connection a Qt:irectionConnection, right?
    May the Lord be with you. Always.

  5. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Safety of emitting a signal with a reference

    From Qt docs:
    Qt:irectConnection: The slot is invoked immediately when the signal is emitted. The slot is executed in the signalling thread.
    So basically it is like a normal function call. But if you want to force DirectConnection type, then why bother with signals / slots ?

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Safety of emitting a signal with a reference

    Yes, but most often the connection is made by code that is external to both sender and receiver, so neither can really rely on the direct connection.

    This requires explicit documentation and that developers read said documentation

    Cheers,
    _

  7. #6
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Safety of emitting a signal with a reference

    Quote Originally Posted by stampede View Post
    then why bother with signals / slots ?
    For improved code organization, of course (no reason to go including header files from classes not directly related to the one in which I'm emitting the signal just to call one of his methods!)

    Quote Originally Posted by anda_skoa View Post
    Yes, but most often the connection is made by code that is external to both sender and receiver, so neither can really rely on the direct connection.
    Ok, so my turn-around solution flew away =( I guess I'll move to shared pointers after all (or including the header file and calling the method I want to use )

    Thanks for your reply!

    Momergil
    May the Lord be with you. Always.

Similar Threads

  1. emitting a signal with no button?
    By kja in forum Newbie
    Replies: 3
    Last Post: 29th November 2010, 21:33
  2. Problem emitting a signal
    By franco.amato in forum Qt Programming
    Replies: 1
    Last Post: 16th December 2009, 00:56
  3. Emitting signal from DLL to EXE
    By Miihkali in forum Qt Programming
    Replies: 6
    Last Post: 27th March 2009, 08:32
  4. Emitting signal causes CRASH
    By navi1084 in forum Qt Programming
    Replies: 7
    Last Post: 12th March 2009, 16:17
  5. cost of emitting signal
    By quickNitin in forum Newbie
    Replies: 1
    Last Post: 29th November 2006, 08:53

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.