I'm creating a application in C++ with the QT framework which includes a HTTP POST request. The value the reply pointer points to doesn't get deleted and causes a memory leak. I have tried to delete this memory allocation like this:
if (reply)
{
qDebug() << "reply deleted";
delete reply;
}
reply = m_qnam->post(request, jsonString);
qDebug() << reply;
connect(reply, SIGNAL(finished()), this, SLOT(handleNetworkData()));
connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(handleSSLErrors()));
Whenever I try to do so, the program crashes instantly. In the header of this class the reply is initiated as a pointer:
QNetworkReply * reply;
Does anyone know why my program crashes by deleting the memory allocation the pointer points to? What would be a possible solution for my problem?
也许你应该使用reply.deleteLater()
Instead of using raw pointers, you might want to consider switching to Smartpointers (such as std::unique_ptr
) instead.
Smartpointers make usage of RAII, in which the Instance of the class std::unique_ptr<T>
holds the pointer and deletes it upon destruction.
To answer your question: it crashes because reply = m_qnam->post(request, jsonString);
may return a nullptr
and you (presumably) try to dereference this nullptr
.
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.