1

From the problem here: Qt Signals and Slots object disconnect? If I first call the delete on a QObject, then I call the disconnect function like this:

MyQClass* A = new MyQClass();
connect(A,SIGNAL(A_S()),this,SLOT(B_S()));
A->deleteLater();
...
disconnect(A,SIGNAL(A_S()),this,SLOT(B_S()));

Will this cause a crash? I found it cause a crash under Qt4, but not Qt5? Is the different Qt version doing something different? Otherwise, there might be something else wrong with my code.

Community
  • 1
  • 1
Nyaruko
  • 4,329
  • 9
  • 54
  • 105
  • 3
    Why would you even do this? The connection will be disconnected automatically. [*`QObject::~QObject(): `All signals to and from the object are automatically disconnected*](http://qt-project.org/doc/qt-5/qobject.html#dtor.QObject). – thuga Nov 27 '14 at 07:55

1 Answers1

2

Connections are disconnected automatically on object destruction. The crash probably happens because you are trying to call disconnect on an object that was destroyed. You are getting a dangling pointer A (having an address to an object that does not exist anymore). But this depends a little an what "..." is.

Silicomancer
  • 8,604
  • 10
  • 63
  • 130
  • I did not do anything with A during the "...", and I confirm the crash is due to the disconnect() since I checked the call stack. Could you kindly explain more about "But this depends a little an what "..." is." – Nyaruko Nov 27 '14 at 08:21
  • You could have changed the pointer A there. Or there could have been some condition. It was only theoretical. Also you need to return to event loop in "..." to trigger the dangling-pointer-bug. – Silicomancer Nov 27 '14 at 09:32