标签: shared-ptr

在DLL接口中使用boost :: shared ptr可以吗?

在C++中开发一个返回boost共享指针并将它们用作参数的DLL是否有效?

那么,导出这样的函数是否可以?

1.) boost::shared_ptr<Connection> startConnection();
2.) void sendToConnection(boost::shared_ptr<Connection> conn, byte* data, int len);
Run Code Online (Sandbox Code Playgroud)

特别说明:引用计数是跨越DLL边界还是需要exe和dll使用相同的运行时?

目的是克服对象所有权的问题.所以当dll和exe都不再引用它时,对象会被删除.

dll boost memory-management shared-ptr

10
推荐指数
1
解决办法
3564
查看次数

为什么enable_shared_from_this嵌入弱指针而不是直接嵌入引用计数器?

enable_shared_from_this助手包含被创建共享指向对象时设置的弱指针.这意味着有一个引用计数(单独分配或与使用的对象一起分配make_shared)和一个额外weak_ptr的对象.

现在为什么不简单地包含引用计数呢?shared_ptr从哑指针设置时,必须完全定义类型,因此shared_ptr构造函数或赋值运算符可以检测从中派生的类型enable_shared_from_this并使用正确的计数器,并且格式可以保持不变,因此复制无关紧要.实际上,shared_ptr已经有必要检测它来设置嵌入式weak_ptr.

c++ shared-ptr c++11 enable-shared-from-this

10
推荐指数
1
解决办法
1236
查看次数

如果条件,shared_ptr如何工作

在C++中,我可以编写如下内容:

shared_ptr<A> a_sp = someFunctionReturningSharedPtr();
if (a_sp) {
    cout << a_sp->someData << endl;
} else {
    cout << "Shared Pointer is NULL << endl;
} 
Run Code Online (Sandbox Code Playgroud)

为什么if (a_sp)检查工作正常?a_sp不是布尔值,但如何检查true或false?if条件如何知道检查a_sp.get()函数的结果?或者,如果没有,是怎么NULL了的,两者均a_sp检查?是否有shared_ptr定义的函数将其转换为布尔值?

c++ shared-ptr

10
推荐指数
1
解决办法
6404
查看次数

为什么不将shared_ptr <A>隐式转换为shared_ptr <A const>?

我试图向const一些新代码介绍一些正确性(实际上是函数范例),并发现我无法将std::shared_ptr<A>一个函数传递给期望的函数std::shared_ptr<A const>.请注意,我不想抛弃 const而是引入它,这对于原始指针是合法的.

有办法解决这个问题吗?我没有找到成员函数来执行此操作.


g ++ 4.6.1发出的确切错误是:

error: no matching function for call to ‘foo(std::shared_ptr<A>)’
note: candidate is:
note: template<class T> std::shared_ptr<_Tp> foo(std::shared_ptr<const _Tp>)
Run Code Online (Sandbox Code Playgroud)

c++ const-correctness shared-ptr c++11

10
推荐指数
1
解决办法
4386
查看次数

如何将对象实例转换为shared_ptr实例

假设我有两个shared_ptr类型,例如

boost::shared_ptr<ObjA> sptrA;
boost::shared_ptr<ObjB> sptrB;
Run Code Online (Sandbox Code Playgroud)

现在假设sptrA->SomeMethod()返回了一个简单的ObjB类型(不是共享ptr).我可以在sptrB中以某种方式存储该类型吗?这样我就可以做这样的事情,以便返回的类型实例自动转换为boost_shared ptr

sptrB = sptrA->SomeMethod(); 
Run Code Online (Sandbox Code Playgroud)

我只是好奇地询问这个问题,是否可能?

c++ boost shared-ptr

10
推荐指数
1
解决办法
9277
查看次数

前向声明和shared_ptr

我正在尝试重构我的代码,以便我使用前向声明而不是包含大量的标头.我是新手,对boost :: shared_ptr有疑问.

说我有以下界面:

#ifndef I_STARTER_H_
#define I_STARTER_H_

#include <boost/shared_ptr.hpp>

class IStarter
{
public:
    virtual ~IStarter() {};

    virtual operator()() = 0;
};

typedef boost::shared_ptr<IStarter> IStarterPtr;

#endif
Run Code Online (Sandbox Code Playgroud)

然后我在另一个类中有一个函数,它将一个IStarterPtr对象作为参数,比如说:

virtual void addStarter(IStarterPtr starter)
{
    _starter = starter;
}
...
IStarterPtr _starter;
Run Code Online (Sandbox Code Playgroud)

如何在不包含IStarter.h的情况下转发声明IStarterPtr?

我正在使用C++ 98,如果这是相关的.

c++ shared-ptr forward-declaration c++98

10
推荐指数
1
解决办法
2万
查看次数

如果我碰巧注意到它已过期,我应该在weak_ptr上调用reset吗?

我有一组Creature对象,使用std::make_shared和创建并拥有在我的应用程序的一部分中std::shared_ptr.

我还跟踪一个选择的零个或一个CreatureWorld使用对象std::weak_ptr<Creature>.

void World::SetSelection(const std::shared_ptr<Creature>& creature) {
    selection = creature;
}

std::shared_ptr<Creature> World::GetSelection() const {
    return selection.lock();
}
Run Code Online (Sandbox Code Playgroud)

调用者GetSelection负责检查指针是否为空.如果是,那意味着目前没有选择.

这一切都完全符合我的喜好:当选定Creature的自然原因(应用程序中的其他地方)死亡时,再次GetSelection开始返回nullptr,就像没有选择任何东西一样.

但是在这种情况下,World::selection成员仍然指向std::shared_ptr控制块.这可能非常大,因为我std::make_shared用来创建我的Creature对象(我意识到Creature对象在正确的时间被正确销毁但仍然分配了它的内存).我正在考虑GetSelection改为:

std::shared_ptr<Creature> World::GetSelection() {
    const auto ret = selection.lock();
    if (!ret)
        selection.reset();

    return ret;
}
Run Code Online (Sandbox Code Playgroud)

一旦我注意到它不再需要,它就会释放内存.令人讨厌的是,这个版本GetSelection不可能const.

我的问题:

  1. GetSelection在这种情况下哪个版本被认为是最佳做法?

  2. 如果在模板化代码中发生类似的事情,那么答案是否会改变,哪些sizeof(T)是未知的并且可能是巨大的?或者在C++ 14中std::make_shared<T[]> …

c++ shared-ptr weak-ptr

10
推荐指数
1
解决办法
1463
查看次数

shared_ptr奇怪性与空值和自定义删除

我们最近在使用自定义删除器从unique_ptr移动到shared_ptr时遇到了崩溃.当用于创建智能指针的指针为空时发生崩溃.下面是重现问题的代码,并显示了两个有效的案例.

在下面的源代码中,One和Two快乐地运行,而Three在"ReleaseDestroy"中崩溃.当智能指针中使用的类具有虚拟"释放"时,崩溃似乎正在发生,因此程序正在尝试查找V表.unique_ptr看起来像检查空指针并且不运行析构函数.共享指针似乎忽略了这一点.

有谁知道这是设计,还是stl实现中的错误?我们正在使用Visual Studio 2015.

#include <iostream>
#include <memory>

template<class R>
void ReleaseDestroy(R* r)
{
    r->Release();
};

class FlatDestroy
{
public :
    void Release()
    {
        delete this;
    }
};

class VirtualDestroy
{
public:
    virtual void Release()
    {
        delete this;
    }
};

class SimpleOne
{
public :
};

void main()
{
    std::shared_ptr<SimpleOne> One(nullptr);
    std::shared_ptr<FlatDestroy> Two(nullptr, ReleaseDestroy<FlatDestroy>);
    std::shared_ptr<VirtualDestroy> Three(nullptr, ReleaseDestroy<VirtualDestroy>);

    One.reset();
    Two.reset();
    Three.reset();
}
Run Code Online (Sandbox Code Playgroud)

c++ shared-ptr unique-ptr

10
推荐指数
1
解决办法
861
查看次数

将shared_ptr分配给数组的偏移量

假设我有一个shared_ptr数组:

std::shared_ptr<int> sp(new T[10], [](T *p) { delete[] p; });
Run Code Online (Sandbox Code Playgroud)

一个方法:

shared_ptr<T> ptr_at_offset(int offset) {
    // I want to return a shared_ptr to (sp.get() + offset) here
    // in a way that the reference count to sp is incremented...
}
Run Code Online (Sandbox Code Playgroud)

基本上,我要做的是返回一个新的shared_ptr,增加引用计数,但指向原始数组的偏移量; 我想避免在调用者在某个偏移处使用数组时删除数组.如果我回来sp.get() + offset可能会发生,对吗?我认为初始化一个新shared_ptr的包含sp.get() + offset也没有意义.

C++的新手,所以不确定我是否正确接近这个.

c++ arrays reference-counting shared-ptr

10
推荐指数
1
解决办法
351
查看次数

通过引用或值作为参数将共享指针传递给类

如果共享指针要被复制到成员变量,是否应该通过引用或值作为参数传递给类?

复制共享指针将增加参考计数,我不想做任何不必要的副本,因此ref计数增量.将共享指针作为参考传递解决这个问题?我认为确实如此,但是还有其他问题吗?

价值传递:

class Boo {
public: 
    Boo() { }
};

class Foo {
public:
    Foo(std::shared_ptr<Boo> boo) 
        : m_Boo(boo) {}
private:
    std::shared_ptr<Boo> m_Boo;
};

int main() {
    std::shared_ptr<Boo> boo = std::make_shared<Boo>();

    Foo foo(boo);
}
Run Code Online (Sandbox Code Playgroud)

通过参考:

class Boo {
public: 
    Boo() { }
};

class Foo {
public:
    Foo(std::shared_ptr<Boo>& boo) 
        : m_Boo(boo) {}
private:
    std::shared_ptr<Boo> m_Boo;
};

int main() {
    std::shared_ptr<Boo> boo = std::make_shared<Boo>();

    Foo foo(boo);
}
Run Code Online (Sandbox Code Playgroud)

c++ shared-ptr

10
推荐指数
1
解决办法
1267
查看次数