Fixed crash due tu accessing already deleted link

This commit is contained in:
Albert Vaca 2013-08-16 07:20:34 +02:00
parent 907740196a
commit 46f2fcfbce

View file

@ -138,12 +138,6 @@ void BroadcastTcpLinkProvider::connected()
TcpDeviceLink* dl = new TcpDeviceLink(id, this, socket);
connect(dl, SIGNAL(destroyed(QObject*)),
this, SLOT(deviceLinkDestroyed(QObject*)));
QMap< QString, DeviceLink* >::iterator oldLinkIterator = links.find(id);
DeviceLink* oldLink = (oldLinkIterator == links.end())? 0 : oldLinkIterator.value();
NetworkPackage np2("");
NetworkPackage::createIdentityPackage(&np2);
@ -154,15 +148,22 @@ void BroadcastTcpLinkProvider::connected()
qDebug() << "Handshaking done (i'm the existing device)";
links[id] = dl;
connect(dl, SIGNAL(destroyed(QObject*)),
this, SLOT(deviceLinkDestroyed(QObject*)));
Q_EMIT onConnectionReceived(*np, dl);
if (oldLink) {
QMap< QString, DeviceLink* >::iterator oldLinkIterator = links.find(id);
if (oldLinkIterator != links.end()) {
DeviceLink* oldLink = oldLinkIterator.value();
disconnect(oldLink, SIGNAL(destroyed(QObject*)),
this, SLOT(deviceLinkDestroyed(QObject*)));
delete oldLink;
oldLink->deleteLater();
links.erase(oldLinkIterator);
}
links[id] = dl;
} else {
//I think this will never happen
qDebug() << "Fallback (2), try reverse connection";
@ -212,26 +213,24 @@ void BroadcastTcpLinkProvider::dataReceived()
const QString& id = np.get<QString>("deviceId");
TcpDeviceLink* dl = new TcpDeviceLink(id, this, socket);
qDebug() << "Handshaking done (i'm the new device)";
connect(dl, SIGNAL(destroyed(QObject*)),
this, SLOT(deviceLinkDestroyed(QObject*)));
QMap< QString, DeviceLink* >::iterator oldLinkIterator = links.find(id);
DeviceLink* oldLink = (oldLinkIterator == links.end())? 0 : oldLinkIterator.value();
links[id] = dl;
//qDebug() << "BroadcastTcpLinkProvider creating link to device" << id << "(" << socket->peerAddress() << ")";
qDebug() << "Handshaking done (i'm the new device)";
Q_EMIT onConnectionReceived(np, dl);
if (oldLink) {
QMap< QString, DeviceLink* >::iterator oldLinkIterator = links.find(id);
if (oldLinkIterator != links.end()) {
DeviceLink* oldLink = oldLinkIterator.value();
disconnect(oldLink, SIGNAL(destroyed(QObject*)),
this, SLOT(deviceLinkDestroyed(QObject*)));
delete oldLink;
oldLink->deleteLater();
links.erase(oldLinkIterator);
}
links[id] = dl;
disconnect(socket,SIGNAL(readyRead()),this,SLOT(dataReceived()));
} else {