Skip to content

Commit

Permalink
QObject: stronger warning about isSignalConnected and threads
Browse files Browse the repository at this point in the history
Document explicitly that it is not allowed to call isSignalConnected
from (dis)connectNotify overrides, and add the respective warning from
the disconnectNotify documentation also to the connectNotify
documentation (with some light editing).

Pick-to: 6.4 6.2
Fixes: QTBUG-106025
Change-Id: I41e8a9d3e6ce697cb2943d55a7c853eeec9c1dbe
Reviewed-by: Lars Knoll <[email protected]>
Reviewed-by: Thiago Macieira <[email protected]>
Reviewed-by: Mårten Nordheim <[email protected]>
  • Loading branch information
vohi committed Nov 26, 2022
1 parent 01dfa22 commit e75c1a0
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/corelib/kernel/qobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2653,13 +2653,15 @@ int QObject::receivers(const char *signal) const
\snippet code/src_corelib_kernel_qobject.cpp 49
As the code snippet above illustrates, you can use this function
to avoid emitting a signal that nobody listens to.
As the code snippet above illustrates, you can use this function to avoid
expensive initialization or emitting a signal that nobody listens to.
However, in a multithreaded application, connections might change after
this function returns and before the signal gets emitted.
\warning This function violates the object-oriented principle of
modularity. However, it might be useful when you need to perform
expensive initialization only if something is connected to a
signal.
modularity. In particular, this function must not be called from an
override of connectNotify() or disconnectNotify(), as those might get
called from any thread.
*/
bool QObject::isSignalConnected(const QMetaMethod &signal) const
{
Expand Down Expand Up @@ -3333,8 +3335,13 @@ bool QObject::disconnect(const QObject *sender, const QMetaMethod &signal,
signal.
\warning This function is called from the thread which performs the
connection, which may be a different thread from the thread in
which this object lives.
connection, which may be a different thread from the thread in which
this object lives. This function may also be called with a QObject internal
mutex locked. It is therefore not allowed to re-enter any QObject
functions, including isSignalConnected(), from your reimplementation. If
you lock a mutex in your reimplementation, make sure that you don't call
QObject functions with that mutex held in other places or it will result in
a deadlock.
\sa connect(), disconnectNotify()
*/
Expand Down Expand Up @@ -3363,12 +3370,12 @@ void QObject::connectNotify(const QMetaMethod &signal)
expensive resources.
\warning This function is called from the thread which performs the
disconnection, which may be a different thread from the thread in
which this object lives. This function may also be called with a QObject
internal mutex locked. It is therefore not allowed to re-enter any
of any QObject functions from your reimplementation and if you lock
a mutex in your reimplementation, make sure that you don't call QObject
functions with that mutex held in other places or it will result in
disconnection, which may be a different thread from the thread in which
this object lives. This function may also be called with a QObject internal
mutex locked. It is therefore not allowed to re-enter any QObject
functions, including isSignalConnected(), from your reimplementation. If
you lock a mutex in your reimplementation, make sure that you don't call
QObject functions with that mutex held in other places or it will result in
a deadlock.
\sa disconnect(), connectNotify()
Expand Down

0 comments on commit e75c1a0

Please sign in to comment.