move assignment

closed account (jGAShbRD)
my_memory.h: In member function ‘my_shared_ptr<T>& my_shared_ptr<T>::operator=(my_shared_ptr<T>&&)’:
my_memory.h:208:13: error: request for member ‘swap’ in ‘((my_shared_ptr<T>*)this)->my_shared_ptr<T>::counter’, which is of non-class type ‘int*’
counter.swap(object.counter);

im getting this error. for my move assigment.
_______________________________________________________________________________
template <typename T>
my_shared_ptr<T> & my_shared_ptr<T>::operator=(my_shared_ptr<T> && object)
{
if (ptr != nullptr)
{
(*counter)--;
if (*counter == 0)
{
delete counter;
counter = nullptr;
delete ptr;
ptr = nullptr;
}
else
{
ptr.swap(object.ptr);
counter.swap(object.counter);
}
return *this;
}
}
 
std::swap(counter, object.counter);
Performing a swap might result in memory leaks because a moved-from object may not reach the end of its lifetime for an arbitrary amount of time.

Given, e.g. two smart pointers that are each the sole owner of an independent resource (their reference counts are 1)
a = std::move(b);
The resource owned by a is not released until the lifetime of b ends, and b's lifetime is arbitrarily long.
Last edited on
Topic archived. No new replies allowed.