Qt Signals and Slots object disconnect?

Anton picture Anton · Feb 13, 2012 · Viewed 21.4k times · Source

I am wondering if i need to disconnect singals and slots if i destroy the signal emitting object. Here is an example:

QAudioOutput * audioOutput = new QAudioOutput(format,mainWindow);
connect(audioOutput,SIGNAL(stateChanged(QAudio::State)),this,SLOT(stateChanged(QAudio::State)));

delete audioOutput;

audioOutput = new QAudioOutput(format,mainWindow);
connect(audioOutput,SIGNAL(stateChanged(QAudio::State)),this,SLOT(stateChanged(QAudio::State)));

Will this automatically disconnect the signal from the old audioOutput, or will it lead to mem leaks or some other undefined behavior ?

Thank you in advance.

Answer

Adrien BARRAL picture Adrien BARRAL · Feb 13, 2012

The signals are automatically disconnected when you call the QObject destructor. Have a look at the Qt documentation: QObject Destructor