DeleteLater and 2 slots
Unsolved
QML and Qt Quick
-
Hi, i wonna ask if i have guaranti that deleteReplay slot will invoke after reciving slots - if i put deleteLater in reciving slots app crashing
void Modbus::ModbusDevice::readOutputs(int addr, int count, QString name, int id, QModbusDataUnit::RegisterType registerType) { devName = name; QModbusDataUnit readUnit(registerType, addr, count); if (auto *reply = modbus->sendReadRequest(readUnit, 1)) { if (!reply->isFinished()) { if(id == 3) { connect(reply, &QModbusReply::finished,this,&ModbusDevice::onReadOutputsFinished); } else connect(reply, &QModbusReply::finished,this,&ModbusDevice::onReadFinished); connect(reply, &QModbusReply::finished, this, &ModbusDevice::deleteReply); } else { delete reply; } } } void Modbus::ModbusDevice::deleteReply() { QModbusReply *reply = qobject_cast<QModbusReply *>(sender()); reply->deleteLater(); }
-
@qtprogrammer123 said in DeleteLater and 2 slots:
QModbusReply *reply = qobject_cast<QModbusReply *>(sender());
reply->deleteLater();You're not checking the pointer: if it's nullptr it will crash.
You're connecting QModbusReply::finished to ModbusDevice::deleteReply and to ModbusDevice::onReadFinished - why don't you simply call deleteLater inside ModbusDevice::onReadFinished?