标签: shared-ptr

在dll-interfaces中使用shared_ptr

我的dll中有一个抽象类.

class IBase {
  protected:
       virtual ~IBase() = 0;
  public:
       virtual void f() = 0;
};
Run Code Online (Sandbox Code Playgroud)

我想进入IBase加载DLL的exe文件.第一种方法是创建以下功能

IBase * CreateInterface();
Run Code Online (Sandbox Code Playgroud)

并添加虚拟函数Release()IBase.

第二种方法是创建另一个功能

boost::shared_ptr<IBase> CreateInterface();
Run Code Online (Sandbox Code Playgroud)

并且不需要任何Release()功能.

问题.

1)在第二种情况下,在dll(而不是在exe文件中)中调用析构函数和内存释放是否正确?

2)如果使用不同的编译器(或不同的设置)编译exe-file和dll ,第二种情况是否能正常工作.

c++ dll boost abstract-class shared-ptr

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

如何使用shared_ptr避免内存泄漏?

请考虑以下代码.

using boost::shared_ptr;
struct B;
struct A{
    ~A() { std::cout << "~A" << std::endl; }
    shared_ptr<B> b;    
};
struct B {
    ~B() { std::cout << "~B" << std::endl; }
    shared_ptr<A> a;
};

int main() {
    shared_ptr<A> a (new A);
    shared_ptr<B> b (new B);
    a->b = b;
    b->a = a;

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

没有输出.没有调用desctructor.内存泄漏.我一直认为智能指针有助于避免内存泄漏.

如果我需要在类中进行交叉引用,我该怎么办?

c++ boost memory-leaks smart-pointers shared-ptr

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

shared_ptr与非指针资源

在C++ 11中是否可以shared_ptr用来控制非指针资源?


可以用来unique_ptr管理非指针资源.这是通过实现自定义删除器类来完成的,该类提供:

  1. typedef {TYPE} pointer;其中{TYPE}是非指针资源类型
  2. operator()(pointer) 它释放了受控资源

...然后unique_ptr使用自定义删除器作为第二个模板参数实例化a .

例如,在Windows下,可以创建一个unique_ptr管理服务控制句柄的程序.此句柄类型不会通过调用释放delete,而是通过调用释放CloseServiceHandle().以下是执行此操作的示例代码:

自定义删除

struct SvcHandleDeleter
{
    typedef SC_HANDLE pointer;
    SvcHandleDeleter() {};

    template<class Other> SvcHandleDeleter(const Other&) {};

    void operator()(pointer h) const
    {
        CloseServiceHandle(h);
    }
};


typedef std::unique_ptr<SC_HANDLE,SvcHandleDeleter> unique_sch;
Run Code Online (Sandbox Code Playgroud)

实例化

unique_sch scm(::OpenSCManagerA(0, 0, SC_MANAGER_ALL_ACCESS));
Run Code Online (Sandbox Code Playgroud)

是否可以用来shared_ptr控制非指针资源?

根据文档,有shared_ptr构造函数重载提供了提供自定义删除类的方法,但是没有一个构造函数接受的资源类型不是指针或指针周围的包装器.

如何才能做到这一点?

c++ shared-ptr c++11

23
推荐指数
2
解决办法
2242
查看次数

shared_ptr与malloc和免费

我在大型应用程序中工作,包含c和cpp.所有文件都保存为cpp扩展名,但代码以c风格编写.我的意思是它是定义结构而不是类通过malloc和realloc和calloc分配内存.最近他们已经安装了boost库所以我打算使用我现有的代码库所以我有一些以下的问题.

  1. 我可以将std :: shared_ptr与malloc和free一起使用.
  2. 如果是,有人能指出我的示例代码库吗?
  3. 如果我在我的应用程序中创建std :: shared_ptr并将此指针传递给另一个使用malloc或calloc的函数,它会影响任何功能吗?

或者换句话说:

对于以下代码,如何使用std :: shared_ptr实现类似功能:

void allocateBlocks(int **ptr, int *cnt)
{
    *ptr = (int*)malloc(sizeof(int) * 10);
    *cnt = 10;
    /*do something*/ 
}

int main()
{
    int *p = NULL;
    int count = 0;
    allocateBlocks(&p, &count);
    /*do something*/

    free(p);
}
Run Code Online (Sandbox Code Playgroud)

我们调用一些函数,它们接受双指针并在其应用程序中填充结构并使用malloc.我们可以将这些指针分配给std :: shared_ptr吗?例如:

typedef struct txn_s
{
    int s;
    int d;
    int *e;
} txn_t;

typedef boost::shared_ptr<txn_t> tpointer;

tpointer((txn_t*)::malloc(sizeof(txn_t),::free));
Run Code Online (Sandbox Code Playgroud)

c++ boost shared-ptr scoped-ptr

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

在gdb中,我可以调用一些类函数,但其​​他函数"无法解析".为什么?

我还没有参与共享指针..我只知道这个概念.我正在尝试调试以下c ++类中的函数,该类存储XML文件的数据(通过xerces库读入).

// header file
class ParamNode;
typedef boost::shared_ptr<ParamNode> PtrParamNode;

class ParamNode : public boost::enable_shared_from_this<ParamNode> {
public:
   ...
   typedef enum { DEFAULT, EX, PASS, INSERT, APPEND } ActionType;
   bool hasChildren() const;
   PtrParamNode GetChildren();
   PtrParamNode Get(const std::string&  name, ActionType = DEFAULT );
protected:
   ....
   ActionType defaultAction_;
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我正在调试一段代码,其中我有一个指向该类的指针的实例ParamNode,并且它被调用paramNode_

PtrParamNode paramNode_;
// read xml file with xerces
paramNode_ = xerces->CreateParamNodeInstance();
// now, the xml data is stored in paramNode_.
std::string probGeo;
// this works in the code, …
Run Code Online (Sandbox Code Playgroud)

c++ debugging boost gdb shared-ptr

23
推荐指数
4
解决办法
2万
查看次数

将带有自定义删除器的unique_ptr移动到shared_ptr

我有一个函数,它使用自定义删除器创建unique_ptr并返回它:

auto give_unique_ptr() {
    auto deleter = [](int* pi) {
        delete pi;
    };
    int* i = new int{1234};
    return std::unique_ptr<int, decltype(deleter)>(i, deleter);
}
Run Code Online (Sandbox Code Playgroud)

在该函数的客户端代码中,我想移动unique_ptr到a shared_ptr,但我不知道如何做到这一点,因为我不知道我的自定义删除器的decltype在函数之外.

我想它应该看起来像这样:

auto uniquePtr = give_unique_ptr();
auto sharedPtr = std::shared_ptr<..??..>(std::move(uniquePtr));
Run Code Online (Sandbox Code Playgroud)

我需要写什么而不是.. ?? ..来获得正确的类型?

如果这是可能的,那么当它的使用计数达到零时,它会shared_ptr表现得很好并调用我在give_unique_ptr()函数内创建的自定义删除器吗?

c++ shared-ptr unique-ptr c++11 c++14

23
推荐指数
2
解决办法
4309
查看次数

为什么shared_ptr <T> :: use_count()返回long而不是unsigned类型?

shared_ptr观察者20.8.2.2.5 C++ 14最终草案(n4296)

   long use_count() const noexcept;
Run Code Online (Sandbox Code Playgroud)

返回:共享所有权的shared_ptr对象数,或者为空时为0 .*this*this*this

[注意:use_count()效率不一定. - 结束说明]

c++ shared-ptr c++14

23
推荐指数
2
解决办法
784
查看次数

unique_ptr的static_pointer_cast替代方案

我知道使用static_pointer_castwith unique_ptr会导致所包含数据的共享所有权.
换句话说,我想做的是:

unique_ptr<Base> foo = fooFactory();
// do something for a while
unique_ptr<Derived> bar = static_unique_pointer_cast<Derived>(foo);
Run Code Online (Sandbox Code Playgroud)

无论如何,这样做的结果是两个unique_ptr不应该同时存在,所以它只是被禁止.
是的,绝对有道理,这就是为什么不存在static_unique_pointer_cast确实存在的原因.

到目前为止,在我想存储指向这些基类的指针的情况下,但我还需要将它们转换为某些派生类(例如,想象一下涉及类型擦除的场景),我使用了shared_ptrs因为我的'如上所述.

无论如何,我猜测是否有替代方案可以shared_ptr解决这样的问题,或者在这种情况下它们是否真的是最佳解决方案.

c++ casting shared-ptr unique-ptr c++11

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

在std :: list <shared_ptr>的C++ 14初始化列表中间抛出GCC(但不是Clang)下的内存泄漏

考虑以下程序:

#include <stdexcept>
#include <stdio.h>
#include <memory>
#include <list>
class Foo {
public:
    Foo(){
        if (s_ct==0) {throw std::bad_alloc();}
        --s_ct;
        fprintf(stderr, "ctor %p\n", this);
    }
    ~Foo(){
        fprintf(stderr, "dtor %p\n", this);
    }
private:
    static int s_ct;
};

int Foo::s_ct = 2;

int main(){
    try {
        std::list<std::shared_ptr<Foo>> l = {
            std::make_shared<Foo>(),
            std::make_shared<Foo>(),
            std::make_shared<Foo>()
        };
    } catch (std::bad_alloc&) {
        fprintf(stderr, "caught exception.\n");
    }
    fprintf(stderr, "done.\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译如下:

[little:~] $ g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, …
Run Code Online (Sandbox Code Playgroud)

c++ exception-handling shared-ptr initializer-list c++14

22
推荐指数
1
解决办法
421
查看次数

使用带有多重继承的enable_shared_from_this

BI正在enable_shared_from_this我的代码中使用,我不确定它的用法是否正确.这是代码:

class A: public std::enable_shared_from_this<A>
{
public:
    void foo1()
    {
        auto ptr = shared_from_this(); 
    }
};

class B:public std::enable_shared_from_this<B>
{
public:
    void foo2()
    {
        auto ptr = shared_from_this(); 
    }
};

class C:public std::enable_shared_from_this<C>
{
public:
    void foo3()
    {
        auto ptr = shared_from_this(); 
    }
};

class D: public A, public B, public C
{
public:
    void foo()
    {
        auto ptr = A::shared_from_this(); 
    }
};
Run Code Online (Sandbox Code Playgroud)

这些用法是否make_shared_from_this()正确,假设它们总是被shared_ptrD 调用?

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

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