I am implementing a class Exporter to perform some export actions. This class derives from QObject. I want to create a pointer to that class on the heap from a const function of the class C (C::triggerExport). I cannot create a unique_ptr as a member of the class C, since I cannot modify it from the triggerExport member.
class Exporter
{
void export()
{
// Do some initialization....
// problem: if an exception is thrown here, the Exporter will never
// be deleted
QDialog * dialog = new QDialog();
connect(dialog, SIGNAL(rejected()), SLOT(deleteLater());
}
};
class C
{
void triggerExport() const
{
//create new here
Exporter * e = new Exporter;
e->export();
}
};
How can I design Exporter in a way that won't cause leaks in the presence of exceptions?