如何将原始指针包装到shared_ptr中,并防止shared_ptr删除对象

How to wrap a raw pointer into a shared_ptr and prevent shared_ptr from deleting the object?

本文关键字:ptr shared 对象 删除 包装 指针 原始      更新时间:2023-10-16

我需要将原始指针包装到shared_ptr中,以便将其传递给函数。函数返回后不再保存对输入对象的任何引用。

{
  MyClass i;
  shared_ptr<MyClass> p(&i);
  f(p);
  // BAD: shared_ptr will delete i.
}

如何阻止shared_ptr删除引用的对象?

正如chris在评论中提到的,写一个空的delete:

#include <type_traits>
template <typename T>
struct empty_delete
{
    empty_delete() /* noexcept */
    {
    }
    template <typename U>
    empty_delete(const empty_delete<U>&,
        typename std::enable_if<
            std::is_convertible<U*, T*>::value
        >::type* = nullptr) /* noexcept */
    {
    }
    void operator()(T* const) const /* noexcept */
    {
        // do nothing
    }
};

使用示例:

#include <iostream>
#include <memory>
struct noisy
{
    noisy() { std::cout << "alive" << std::endl; }
    ~noisy() { std::cout << "dead" << std::endl; }
    noisy(const noisy&);
    noisy& operator=(const noisy&);
};
template <typename T>
void take(T& yours)
{
    std::cout << "Taking..." << std::endl;
    {
        auto mine = std::move(yours);
    }
    std::cout << "Took." << std::endl;
}
int main()
{
    std::unique_ptr<noisy> a(new noisy());
    std::shared_ptr<noisy> b(new noisy());
    std::unique_ptr<noisy, empty_delete<noisy>> c(new noisy());
    std::shared_ptr<noisy> d(new noisy(), empty_delete<noisy>());
    take(a);
    take(b);
    take(c);
    take(d);
}
输出:


活着活着
活着
活着
把…

了。
把…

了。
把…
了。
把…
花了。

当然,这个例子泄漏了内存

对于评论中提到的。net/clr,您应该实现ref class,传递托管句柄^,并让垃圾收集器管理生命周期。

ref class MyClassManaged : public Pen
{
public:
  MyClassManaged() : Pen{}, native_{ new MyClass{} } { }
  ~MyClassManaged() { this->!MyClassManaged(); }
  !MyClassManaged() { delete native_; }
private:
  MyClass* native_;
};
//...
{
  MyClassManaged^ i = gcnew MyClassManaged{};
  fManaged(i);
}

TL;DR只需保留shared_ptr<MyClass>的另一个副本,如果仍然需要,将i分配到堆上而不是堆栈上。

场景1

{
  shared_ptr<MyClass> another_p;
  {
    MyClass i;
    shared_ptr<MyClass> p(&i);
    f(p);
    // p has not deleted i yet
    another_p = p; // increment reference count
    // p will decrement the reference count
    // i will be deleted due to stack unwinding
  }
  // another_p still holds a reference to where i was on the stack
  if (another_p)
    something(); // yes, something() will be invoked
  else
    nothing(); // no, nothing() won't run here
  // another_p will attempt to delete again if not the shared_ptr is not copied elsewhere
}

场景2

{
  shared_ptr<MyClass> another_p;
  {
    auto p = make_shared<MyClass>();
    auto& i = *p;
    f(p);
    // p has not deleted i
    another_p = p; // increment reference count
    // p will decrement the reference count
    // i will be NOT deleted
  }
  // another_p still holds a reference to where i was on the heap
  if (another_p)
    something(); // yes, something() will be invoked
  else
    nothing(); // no, nothing() won't run here
  // another_p will attempt to delete again if not the shared_ptr is not copied elsewhere
}
// it will finally be deleted now

简单地保留另一个shared_ptr的副本;不需要使用空的删除器。