简体   繁体   中英

Pass QNetworkReply pointer to QSharedPointer

One question about use of QSharedPointer in following scenario:

I have a class with two QSharedPointer s private class members:

class xy{

...

private:
    QSharedPointer<QNetworkAccessManager>   m_nam;
    QSharedPointer<QNetworkReply>           m_nr;
};

In the code I send a QNetworkAccessManager post, the return value is a QNetworkReply pointer.

QNetworkReply * QNetworkAccessManager::post(const QNetworkRequest & request, const QByteArray & data)

I want to to pass this pointer to my QSharedPointer ie m_nr . I tried:

m_nam.reset( new QNetworkAccessManager(m_parent.data()));
QNetworkRequest request(url);
...
...
// following line is my problem
m_nr = QSharedPointer<QNetworkReply>(m_nam->post(request, postData));
...

But crashed, then I tried:

m_nam.reset( new QNetworkAccessManager(m_parent.data()));
QNetworkRequest request(url);
...
...
// following line is my problem
m_nr.reset( m_nam->post(request, postData));
...

But also crashed.

How to pass QNetworkReply pointer correctly to my QSharedPointer ?

What is the error on crash?? Is it exactly crashing at the line you mentioned.

Be carefull in Qt to combine smart pointers and QObjects parenting. Parenting in Qt affects object freeing with some kind of pseudo garbage collection. All children of a deleted QObject are deleted as well. Combining this with QSharedPointer for example might result in multiple object free or access after free kind of problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM