Skip to content

Commit

Permalink
fix(parented_ptr): don't leak memory when managed object has no parent
Browse files Browse the repository at this point in the history
  • Loading branch information
Swiftb0y committed Jun 24, 2024
1 parent 2507d84 commit f031f51
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/util/parented_ptr.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <qlogging.h>
#include <qobject.h>
#ifdef MIXXX_DEBUG_ASSERTIONS_ENABLED
#include <QObject>
#endif
Expand Down Expand Up @@ -29,16 +31,15 @@ class parented_ptr final {
explicit parented_ptr(T* t) noexcept
: m_ptr{t} {
}
// Only generate destructor if not empty, otherwise its empty but will
// cause the parented_ptr to be not trivially destructible even though it could be.
#ifdef MIXXX_DEBUG_ASSERTIONS_ENABLED
~parented_ptr() noexcept {
DEBUG_ASSERT(!m_ptr || static_cast<const QObject*>(m_ptr)->parent());
if (!m_ptr) {
return;
}
VERIFY_OR_DEBUG_ASSERT(static_cast<const QObject*>(m_ptr)->parent()) {
// managed object has no parent that could clean it up later, thus we're responsible.
delete m_ptr;
}
}
#else
// explicitly generate trivial destructor (since decltype(m_ptr) is not a class type)
~parented_ptr() noexcept = default;
#endif
// Rule of 5
parented_ptr(const parented_ptr<T>&) = delete;
parented_ptr& operator=(const parented_ptr<T>&) = delete;
Expand Down

0 comments on commit f031f51

Please sign in to comment.