Fixed memory leak in move assignement operators

This commit is contained in:
MHaselmaier 2021-05-03 21:59:43 +02:00
parent 2b6a90f601
commit 4f1e0b81d9
2 changed files with 27 additions and 11 deletions

View File

@ -168,23 +168,16 @@ CoroutineStarter::CoroutineStarter(CoroutineStarter&& other)
}
CoroutineStarter::~CoroutineStarter() {
if(m_first != nullptr) {
auto curr = m_first;
while(curr != nullptr) {
AbstractCoroutine* next = nullptr;
if(curr->m_parentReturnAction.m_type == Action::TYPE_COROUTINE) {
next = curr->m_parentReturnAction.m_data.coroutine;
}
delete curr;
curr = next;
}
}
freeCoroutines();
}
/*
* Move assignment operator.
*/
CoroutineStarter& CoroutineStarter::operator=(CoroutineStarter&& other) {
if (this == std::addressof(other)) return *this;
freeCoroutines();
m_first = other.m_first;
m_last = other.m_last;
other.m_first = nullptr;
@ -216,6 +209,21 @@ CoroutineStarter& CoroutineStarter::next(CoroutineStarter&& starter) {
return *this;
}
void CoroutineStarter::freeCoroutines()
{
if (m_first != nullptr) {
auto curr = m_first;
while (curr != nullptr) {
AbstractCoroutine* next = nullptr;
if (curr->m_parentReturnAction.m_type == Action::TYPE_COROUTINE) {
next = curr->m_parentReturnAction.m_data.coroutine;
}
delete curr;
curr = next;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CoroutineHandle

View File

@ -341,6 +341,11 @@ class CoroutineStarter {
private:
AbstractCoroutine* m_first;
AbstractCoroutine* m_last;
private:
void freeCoroutines();
public:
/**
@ -686,6 +691,9 @@ public:
* Move assignment operator.
*/
StarterForResult& operator=(StarterForResult&& other) {
if (this == std::addressof(other)) return *this;
delete m_coroutine;
m_coroutine = other.m_coroutine;
other.m_coroutine = nullptr;
return *this;