microbit: BUGFIX: Memory leak in ManagedType.h
Corrected corner case memory leak. Instances of ManagedType<> created using the default constructor would create a reference counter by default, with a value of 0. However, if destructor or copy-assigned in this state, this reference count would not be destroyed, resulting in a 4 byte leak on the heap for each occurence.
This commit is contained in:
parent
78ad8a1646
commit
a61bd49620
1 changed files with 18 additions and 2 deletions
|
@ -136,7 +136,16 @@ ManagedType<T>::ManagedType(const ManagedType<T> &t)
|
|||
template<typename T>
|
||||
ManagedType<T>::~ManagedType()
|
||||
{
|
||||
if (--(*ref) == 0)
|
||||
// Special case - we were created using a default constructor, and never assigned a value.
|
||||
if (*ref == 0)
|
||||
{
|
||||
// Simply destroy our reference counter and we're done.
|
||||
free(ref);
|
||||
}
|
||||
|
||||
// Normal case - we have a valid piece of data.
|
||||
// Decrement our reference counter and free all allocated memory if we're deleting the last reference.
|
||||
else if (--(*ref) == 0)
|
||||
{
|
||||
delete object;
|
||||
free(ref);
|
||||
|
@ -152,7 +161,14 @@ ManagedType<T>& ManagedType<T>::operator = (const ManagedType<T>&t)
|
|||
if (this == &t)
|
||||
return *this;
|
||||
|
||||
if (--(*ref) == 0)
|
||||
// Special case - we were created using a default constructor, and never assigned a value.
|
||||
if (*ref == 0)
|
||||
{
|
||||
// Simply destroy our reference counter, as we're about to adopt another.
|
||||
free(ref);
|
||||
}
|
||||
|
||||
else if (--(*ref) == 0)
|
||||
{
|
||||
delete object;
|
||||
free(ref);
|
||||
|
|
Loading…
Reference in a new issue