Backstory:
Solitamente evito Pointers come la peste, e quindi sono molto inesperto nell'usarli. Attualmente sto eseguendo il threading di una classe e si lamenta quando ho un QProcess nello stack perché Cannot create children for a parent that is in a different thread . Questi errori scompaiono quando li metto nell'heap, quindi è quello che sto provando.
Domanda:
Qual è il modo corretto per eliminare e deallocare questo processo?
Il mio primo pensiero è stato quello di impostare parent come classe ( this ) a cui apparteneva:
QEventLoop skuEvent;
m_QuerySku = new QProcess(this); // m_QuerySku is a pointer.
connect(m_QuerySku, SIGNAL(readyRead()), this, SLOT(querySku()));
connect(this, SIGNAL(queriedSku()), &skuEvent, SLOT(quit()));
m_QuerySku->start("ssh", sl);
skuEvent.exec();
disconnect(m_QuerySku, SIGNAL(finished(int)), this, SLOT(querySku()));
disconnect(this, SIGNAL(queriedSku()), &skuEvent, SLOT(quit()));
// Presumably gets deleted when the class it belongs to goes out of scope.
Ho chiesto in ## C ++ su IRC; qualcuno ha suggerito questo:
if
m_QuerySkuis a pointer then that will be deleted when out of scope, howeverQProcesswill still be allocated if you rundelete m_QuerySku;it will justdeleteQProcessallocation, the pointer will stay and I'd recommend setting it tonullptr.
Che interpreto come questo:
QEventLoop skuEvent;
m_QuerySku = new QProcess();
connect(m_QuerySku, SIGNAL(readyRead()), this, SLOT(querySku()));
connect(this, SIGNAL(queriedSku()), &skuEvent, SLOT(quit()));
m_QuerySku->start("ssh", sl);
skuEvent.exec();
disconnect(m_QuerySku, SIGNAL(finished(int)), this, SLOT(querySku()));
disconnect(this, SIGNAL(queriedSku()), &skuEvent, SLOT(quit()));
m_QuerySku.close();
delete m_QuerySku;
m_QuerySku = nullptr;
- Sono entrambi metodi sicuri?
- Il secondo viola le linee guida per lo stile
Qt, o va bene così?
Grazie.