Commit ceececb8 authored by David Faure's avatar David Faure
Browse files

Fix memory leak due to recent port to std::unique_ptr

commit 8eec3450 replaced delete d_ptr
with a std::unique_ptr, but there's a catch in this class hierarchy,
as indicated by the comment about BasePrivate: its destructor isn't
virtual (on purpose) so one needs to delete a pointer with the right
derived type.

Detected by ASAN on bin/utiltest (and probably all others)
parent 604b68c5
Pipeline #91009 passed with stage
in 5 minutes and 50 seconds
......@@ -50,6 +50,8 @@ Content::~Content()
Q_D(Content);
qDeleteAll(d->headers);
d->headers.clear();
delete d_ptr;
d_ptr = nullptr;
}
bool Content::hasContent() const
......
......@@ -668,7 +668,7 @@ protected:
virtual QByteArray assembleHeaders();
//@cond PRIVATE
std::unique_ptr<ContentPrivate> const d_ptr;
ContentPrivate *d_ptr;
//@endcond
private:
......
......@@ -57,7 +57,11 @@
{ \
} \
\
subclass::~subclass() = default;
subclass::~subclass() { \
Q_D(subclass); \
delete d; /* see comment above the BasePrivate class */ \
d_ptr = nullptr; \
}
// end kmime_mk_trivial_ctor_with_dptr
......@@ -97,7 +101,11 @@ Base::Base(BasePrivate *dd) :
{
}
Base::~Base() = default;
Base::~Base()
{
delete d_ptr;
d_ptr = nullptr;
}
void Base::from7BitString(const char *s, size_t len)
{
......@@ -153,7 +161,12 @@ Unstructured::Unstructured() : Base(new UnstructuredPrivate)
{
}
Unstructured::~Unstructured() = default;
Unstructured::~Unstructured()
{
Q_D(Unstructured);
delete d;
d_ptr = nullptr;
}
void Unstructured::from7BitString(const QByteArray &s)
{
......@@ -205,7 +218,13 @@ Structured::Structured() : Base(new StructuredPrivate)
}
kmime_mk_dptr_ctor(Structured, Base)
Structured::~Structured() = default;
Structured::~Structured()
{
Q_D(Structured);
delete d;
d_ptr = nullptr;
}
void Structured::from7BitString(const char *s, size_t len)
......@@ -1173,7 +1192,12 @@ Generic::Generic(const char *t, int len) : Generics::Unstructured(new GenericPri
setType(t, len);
}
Generic::~Generic() = default;
Generic::~Generic()
{
Q_D(Generic);
delete d;
d_ptr = nullptr;
}
void Generic::clear()
{
......
......@@ -35,7 +35,7 @@
#include <QVector>
#include <QByteArray>
#include <QMetaType>
#include <memory>
namespace KMime
{
......@@ -190,7 +190,7 @@ protected:
QByteArray typeIntro() const;
//@cond PRIVATE
std::unique_ptr<BasePrivate> const d_ptr;
BasePrivate *d_ptr;
kmime_mk_dptr_ctor(Base)
//@endcond
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment