i wanna write a qt c++ lib using the pimpl idiom.
Based on this post How to use the Qt's PIMPL idiom? i have written a little program.
Compiling and running is ok but if i wanna close the client with deleteLater()
the Prog raise an error:
Client: destroy instance!
ClientPrivate: destroy instance!
double free or corruption (out)
Child terminated with signal = 0x6 (SIGABRT)
I (must) using qt5.6. gcc9, debian buster (and windows later). i'm new in the world of qt's c++ framwork and I unfortunately do not know 100% the internal mechanisms of Qt... where is my problem?
thx!
// public header
namespace X
{
namespace Y
{
class GCDE_DLL_EXPORT Client; // Marco generated by CMake
class ClientPrivate;
class Client: public QObject
{
Q_OBJECT
Q_DECLARE_PRIVATE(Client)
QScopedPointer<ClientPrivate> d_ptr;
public:
Client(QObject* parent = Q_NULLPTR);
~Client();
};
}
}
// private header
namespace X
{
namespace Y
{
class ClientPrivate
{
// disable copy ctr's
Q_DISABLE_COPY(ClientPrivate)
// Ptr to interface
Q_DECLARE_PUBLIC(Client)
Client* q_ptr;
public:
ClientPrivate(Client* interf);
~ClientPrivate();
};
}
}
// impl cpp
namespace X
{
namespace Y
{
ClientPrivate::ClientPrivate(Client* interf):
q_ptr(interf)
{}
ClientPrivate::~ClientPrivate()
{
qDebug() << "ClientPrivate: destroy instance!";
}
Client::Client(QObject* parent): QObject(parent)
d_ptr(new ClientPrivate(this))
{}
Client::~Client()
{
qDebug() << "Client: destroy instance!";
}
}
}
// tester
#include <QtCore/QObject>
#include <QtCore/QTimer>
#include <QtCore/QCoreApplication>
#include <socket.hpp>
int main(int argc, char *argv[])
{
using namespace X;
QCoreApplication qapp(argc, argv);
Y::Client clt;
QTimer::singleShot(2000, &clt, SLOT(deleteLater())); // after 2s -> raise double free
return qapp.exec();
}