[Interest] QMetaMethod::invoke / Q_ARG with a temporary

Sze Howe Koh szehowe.koh at gmail.com
Wed May 9 12:27:14 CEST 2018


On 8 May 2018 at 04:12, Giuseppe D'Angelo <giuseppe.dangelo at kdab.com> wrote:
> Il 07/05/2018 20:24, Christian Ehrlicher ha scritto:
>>
>> I did not found a hint int the documentation about this behavior.
>> Therefore my question - should this work for a queued connection or not?
>> At least msvc2017 and gcc 4.8 don't like it when compiling with -O2...
>
>
> The documentation doesn't talk about QArgument, but about Q_ARG and
> Q_RETURN_ARG, which is what you should use.
>
> (Under the hood, the problem is that QArgument simply stores a pointer to
> your variable. If that variable is a temporary, that pointer becomes
> immediately dangling. For this reason, do not store what Q_ARG builds, e.g.
> in an variable declared auto!)

>From Qt 5.10 onwards, we can even do without Q_ARG (and QArgument).
Pass a function pointer or lambda expression to
QMetaObject::invokeMethod().


Regards,
Sze-Howe



More information about the Interest mailing list