I'm starting to use the Pimpl idiom (aka cheshire cat, d-pointer) but I'm getting segfaults in my destructors under certain conditions. Let me try to explain:
//MYCLASS.H
class MyClassPrivate;
class MyClass {
public:
MyClass();
MyClass(const MyClass &other);
~MyClass();
private:
MyClassPrivate *d;
}
//MYCLASS.H
class MyClassPrivate;
class MyClass {
public:
MyClass();
MyClass(const MyClass &other);
~MyClass();
private:
MyClassPrivate *d;
}
To copy to clipboard, switch view to plain text mode
//MYCLASS.CPP
#include "myclass.h"
#include <QString>
#include <QMap>
class MyClassPrivate {
public:
QMap<QString, int> mymap;
};
MyClass::MyClass()
: d(new MyClassPrivate)
{ }
MyClass:MyClass(const MyClass &other)
: d(newMyClassPrivate(*other.d))
{ }
MyClass::~MyClass()
{
delete d;
}
//MYCLASS.CPP
#include "myclass.h"
#include <QString>
#include <QMap>
class MyClassPrivate {
public:
QString mystring;
QMap<QString, int> mymap;
};
MyClass::MyClass()
: d(new MyClassPrivate)
{ }
MyClass:MyClass(const MyClass &other)
: d(newMyClassPrivate(*other.d))
{ }
MyClass::~MyClass()
{
delete d;
}
To copy to clipboard, switch view to plain text mode
I have another class, let's call it MyContainerThingy that also uses Pimpl and stores a MyClass value:
//MYCONTAINERTHINGY.H
class MyContainerThingyPrivate;
class MyContainerThingy {
public:
MyContainerThingy();
MyContainerThingy(const MyContainerThingy &other);
~MyContainerThingy();
private:
MyContainerThingyPrivate *d;
}
//MYCONTAINERTHINGY.H
class MyContainerThingyPrivate;
class MyContainerThingy {
public:
MyContainerThingy();
MyContainerThingy(const MyContainerThingy &other);
~MyContainerThingy();
private:
MyContainerThingyPrivate *d;
}
To copy to clipboard, switch view to plain text mode
//MYCONTAINERTHINGY.CPP
#include "mycontainerthingy.h"
#include "myclass.h"
class MyContainerThingyPrivate {
public:
MyClass myclass;
};
MyContainerThingy::MyContainerThingy()
: d(new MyContainerThingyPrivate)
{ }
MyContainerThingy:MyContainerThingy(const MyContainerThingy &other)
: d(newMyContainerThingyPrivate(*other.d))
{ }
MyContainerThingy::~MyContainerThingy()
{
delete d;
}
//MYCONTAINERTHINGY.CPP
#include "mycontainerthingy.h"
#include "myclass.h"
class MyContainerThingyPrivate {
public:
MyClass myclass;
};
MyContainerThingy::MyContainerThingy()
: d(new MyContainerThingyPrivate)
{ }
MyContainerThingy:MyContainerThingy(const MyContainerThingy &other)
: d(newMyContainerThingyPrivate(*other.d))
{ }
MyContainerThingy::~MyContainerThingy()
{
delete d;
}
To copy to clipboard, switch view to plain text mode
These two classes are compiled into a shared library. When I attempt to use these classes from my application it segfaults with the following frame stack (from KDevelop3 debugging via gdb):
#0 QBasicAtomicInt::deref
#1 ~QMap
#2 ~MyClassPrivate
#3 ~MyClass
#4 ~MyContainerThingy
#5 ~MyContainer
#6 main
#0 QBasicAtomicInt::deref
#1 ~QMap
#2 ~MyClassPrivate
#3 ~MyClass
#4 ~MyContainerThingy
#5 ~MyContainer
#6 main
To copy to clipboard, switch view to plain text mode
And it's not only QMap, but QHash, QString as well.
Any ideas?
Bookmarks