0

One question about use of QSharedPointer in following scenario:

I have a class with two QSharedPointers 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 i.e. 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?

jmattheis
  • 10,494
  • 11
  • 46
  • 58

1 Answers1

0

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.

M0rk
  • 56
  • 1
  • Yes, it crashes exactly at this line i mentioned. For the moment i dont use smart pointer but QNetworkReply pointer instead to pass the result from "m_nam->post()". Because of no time at the moment. I will later on continue with this issue... – user3728686 Aug 09 '17 at 10:51