I am reading an introduction book to Qt and I am writing this code:
server.h
class server : public QObject {
Q_OBJECT
private:
QTcpServer* chatServer;
public:
explicit server(QObject *parent = nullptr) : QObject(parent) {}
//more code...
};
#endif // SERVER_H
Then the book suggests to create the object in this way:
chatServer = new QTcpServer(); //<-- ?)
chatServer->setMaxPendingConnections(10);
I have read online that Qt has an hierarchy because everything descends frm QObject
and the parent object will take care of the life of the children. In this case, doesnt the code generate a memory leak?
Because chatServer = new QTcpServer();
is equal to chatServer = new QTcpServer(nullptr);
and so I am not specifying a parent! So there is nobody that takes care of the life of this object in the heap. Do I have to call the delete
(or better use an unique_ptr ) to manage the memory?
Another fix that I'd use would be chatServer = new QTcpServer(server);
. Would this be ok?