shared_ptr的问题。我不能毫无错误地创建我的结构

请帮助查找我的自定义共享指针类中的错误SharedPtr<T>。我找不到它了。但是代码可以处理泄漏。

我认为是重置方法或operator=方法中的错误。可能无需调用这些方法代码即可工作。我认为是因为 valgrind 没有显示任何泄漏和错误。但是调用这些方法会显示错误,有时会泄漏

#include <iostream>
#include <algorithm>
#include <memory>

using std::cout;
using std::cin;
using std::endl;

template <typename T>
struct SharedPtr
{
    explicit SharedPtr(T *ptr = 0) : p_(ptr), c_(0)
    {
        if (p_) c_ = new size_t(1);
    }
    ~SharedPtr()
    {
        if (c_ && --*c_ == 0) 
        {
            delete p_; 
            delete c_;
        }
    }
    SharedPtr(const SharedPtr &p) : p_(p.get()), c_(p.getc()) 
    {
        if (c_) { ++*c_; }
    }
    SharedPtr& operator=(const SharedPtr &p)
    {
        p_ = p.p_;
        c_ = p.c_;
        return *this;
    }

    T* get() const { return p_;}
    size_t* getc() const { return c_; }
    size_t getcc() const { return *c_; }

    void reset(T *ptr = 0)
    {
        SharedPtr(ptr).swap(*this);
        // if (c_ && --*c_ == 0) 
        // {
        //     delete p_; 
        //     delete c_;
        // }
        // if (ptr == nullptr)
        // {
        //     c_ = nullptr;
        // }
        // p_ = ptr;
    }

    T& operator*() const { return *p_;}
    T* operator->() const { return p_;}
private:
    void swap(SharedPtr& other)
    {
        std::swap(p_, other.p_);
        size_t* tmp = c_;
        c_ = other.c_;
        other.c_ = tmp;
    }
    T * p_;
    size_t * c_;
};

int main ()
{
    SharedPtr<int> p1(new int(5));
    SharedPtr<int> p2(new int(10));
    SharedPtr<int> p5;
    p5 = p2;
    p1 = p5;
    p1.reset();
}

valgrind 输出:

==12777== HEAP SUMMARY:
==12777==     in use at exit: 12 bytes in 2 blocks
==12777==   total heap usage: 5 allocs, 3 frees, 72,728 bytes allocated
==12777==
==12777== LEAK SUMMARY:
==12777==    definitely lost: 12 bytes in 2 blocks
==12777==    indirectly lost: 0 bytes in 0 blocks
==12777==      possibly lost: 0 bytes in 0 blocks
==12777==    still reachable: 0 bytes in 0 blocks
==12777==         suppressed: 0 bytes in 0 blocks
==12777== Rerun with --leak-check=full to see details of leaked memory
==12777==
==12777== For lists of detected and suppressed errors, rerun with: -s
==12777== ERROR SUMMARY: 6 errors from 6 contexts (suppressed: 0 from 0)

还有一个同样的错误:

==12777== Invalid read of size 8
==12777==    at 0x1093F9: SharedPtr<int>::~SharedPtr() (in /home/baseoleph/git/stepik/1_7_CPP/a.out)
==12777==    by 0x109296: main (in /home/baseoleph/git/stepik/1_7_CPP/a.out)
==12777==  Address 0x4da5d70 is 0 bytes inside a block of size 8 free'd
==12777==    at 0x483D1CF: operator delete(void*, unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==12777==    by 0x10944D: SharedPtr<int>::~SharedPtr() (in /home/baseoleph/git/stepik/1_7_CPP/a.out)
==12777==    by 0x1094DA: SharedPtr<int>::reset(int*) (in /home/baseoleph/git/stepik/1_7_CPP/a.out)
==12777==    by 0x10928A: main (in /home/baseoleph/git/stepik/1_7_CPP/a.out)
==12777==  Block was alloc'd at
==12777==    at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==12777==    by 0x1093C0: SharedPtr<int>::SharedPtr(int*) (in /home/baseoleph/git/stepik/1_7_CPP/a.out)
==12777==    by 0x109242: main (in /home/baseoleph/git/stepik/1_7_CPP/a.out)

回答

您的赋值运算符不应该减少 c_ 并可能释放 p_ 吗?
并增加新的 c_ 值;


以上是shared_ptr的问题。我不能毫无错误地创建我的结构的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>