[Qt-interest] Safety of Passing by Reference with SignalsandSlots

Will Rutherdale (rutherw) rutherw at cisco.com
Fri May 1 22:15:23 CEST 2009


I'm actually running on a Linux environment.

I just realized, the reason the message got through is that I did use
Q_DECLARE_METATYPE and qRegisterMetaType on that struct.  The reason is
that I exposed slots with that structure to DBus elsewhere in the code.
(I also defined marshalling and demarshalling to boot, but emit probably
doesn't require that part.)

Thanks for the answers.  You've been very helpful.

-Will


-----Original Message-----
From: qt-interest-bounces at trolltech.com
[mailto:qt-interest-bounces at trolltech.com] On Behalf Of Thiago Macieira
Sent: 1 May 2009 15:59
To: qt-interest at trolltech.com
Subject: Re: [Qt-interest] Safety of Passing by Reference with
SignalsandSlots

Will Rutherdale (rutherw) wrote:
>Thanks, that sounds reassuring.  It sounds like you're saying it will
>copy my parameter data into the queue on an emit, so I can pass by
>reference and be safe.  It sounds like you're saying it allocates a
>QEvent object and frees it, or something equivalent.

That's correct :-)

>Regarding your criteria, my struct would have default copy
constructors,
>but had no Q_DECLARE_METATYPE or qRegisterMetaType.  I am familiar with
>these names, as I have used them in other parts of the code involving
>QtDBus.  However, for the simple emit statements that are used within
>one process to talk to different threads, I have not used them.

They're only necessary for queued emissions, which is your case here, 
exactly because Qt needs to copy the arguments.

>When I actually tested the code, it worked.  All the data got through.
>There were no warnings or error messages at any stage.  What is
supposed
>to go wrong if I don't provide Q_DECLARE_METATYPE or qRegisterMetaType?

The signal connection will work without a hitch.

However, the signal emission will cause a warning to be emitted and the 
signal will not be delivered.

Please pay attention to the output of your application, especially if 
you're on Windows (where by default the output is not visible without 
special tools). To catch those cases, I recommend running your
application 
with the QT_FATAL_WARNINGS environment variable set to 1 while
developing.

-- 
Thiago Macieira - thiago.macieira (AT) nokia.com
  Senior Product Manager - Nokia, Qt Software
      Sandakerveien 116, NO-0402 Oslo, Norway




More information about the Qt-interest-old mailing list