相关疑难解决方法(0)

使用boost :: shared_ptr时有什么潜在的危险?

使用时,你有什么方法可以用脚射击自己boost::shared_ptr?换句话说,当我使用时,我必须避免哪些陷阱boost::shared_ptr

c++ boost pointers shared-ptr

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

std :: shared_ptr初始化:make_shared <Foo>()vs shared_ptr <T>(new Foo)

有什么区别:

std::shared_ptr<int> p = std::shared_ptr<int>( new int );
Run Code Online (Sandbox Code Playgroud)

std::shared_ptr<int> p = std::make_shared< int >();
Run Code Online (Sandbox Code Playgroud)

我应该选择哪一个?为什么?

PS相当肯定这已经得到了答案,但我找不到类似的问题.

c++ smart-pointers shared-ptr c++11

43
推荐指数
3
解决办法
6万
查看次数

使用带有多重继承的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万
查看次数

使用boost :: enable_shared_from_this时出现不完整的类型错误

在以下行

class Symbol : public boost::enable_shared_from_this<Symbol> {

我收到错误:

错误:无效使用不完整类型struct boost::enable_shared_from_this<Symbol> /usr/include/boost/smart_ptr/shared_ptr.hpp:63:错误:声明struct boost::enable_shared_from_this<Symbol>

知道为什么我会收到这个错误.符号是一个抽象类(如果重要的话)

c++ shared-ptr

9
推荐指数
1
解决办法
3343
查看次数

对enable_shared_from_this有什么需求?

我是C++ 11的新手,我遇到了enable_shared_from_this.我不明白它想要达到的目的是什么?所以我有一个使用enable_shared_from_this的程序.

struct TestCase: enable_shared_from_this<TestCase>
{
std::shared_ptr<testcase> getptr() {
    return shared_from_this();
}

~TestCase() { std::cout << "TestCase::~TestCase()"; }
};


int main()
{
std::shared_ptr<testcase> obj1(new TestCase);
std::shared_ptr<testcase> obj2 = obj1->getptr();
// The above can be re written as below
//  std::shared_ptr<testcase> obj2 = shared_ptr<testcase>(obj1);
}
Run Code Online (Sandbox Code Playgroud)

我的问题是当我需要一个指向'this'的指针时,为什么不使用obj本身.为什么要从该类的函数返回'this',比如使用getptr()然后返回shared_from_this()???? 我不明白.

第二个问题,如果不使用enable_shared_from_this,为什么dtor被调用两次会产生问题,崩溃!!!!

我可以绕过使用enable_shared_from_this的另一种方法是这样的.在TestCase类中添加它

  std::shared_ptr getptr1(shared_ptr obj) {
   return std::shared_ptr(obj);
  }
Run Code Online (Sandbox Code Playgroud)

从主要打电话给这个:

  std::shared_ptr bp2 = bp1->getptr1(bp1);
Run Code Online (Sandbox Code Playgroud)

并做了.我们不需要enable_shared_from_this.为什么在地球上我们需要它?

c++ boost shared-ptr c++11

5
推荐指数
2
解决办法
1036
查看次数

Boost :: shared_ptr使用它实例化

考虑以下情况:

typedef boost::shared_ptr<B> BPtr;

class A
{
public:
    A() { b_ptr = BPtr(new B); }
    void a_func() { C::c_func(b_ptr); }
private:
    BPtr b_ptr;
}

class B
{
public:
    B() { b_ptr = BPtr(this); }
    void b_func() { C::c_func(b_ptr); }
private:
    BPtr b_ptr;
}

class C
{
public:
    static void c_func(BPtr b_ptr) { /*...*/ }
}
Run Code Online (Sandbox Code Playgroud)

可以用shared实例化shared_ptr this吗?
是否可以让两个shared_ptr对象指向同一个对象?(例如A :: b_ptr和B :: b_ptr)
如果这两个中的一个超出范围 - B的实例是否会被删除?

我猜我在做一些根本错误的事情.
我还考虑过将b_ptr的依赖注入用于B的构造函数,但这似乎也是非常错误的.

更新:
澄清 - A和B都需要使用C :: c_func.反过来,经过一些处理后,C需要在B中调用一个我没有在上面指定的回调函数.实际上有两个案例很有意思:

  1. 如果要求C不是有状态的 - 那么它需要从A和B中接收BPtr,如上面的代码所示.
  2. 如果C是有状态的,并且A和B都实例化单独的C实例,则在C的ctor中给出BPtr.

c++ multiple-instances this shared-ptr

3
推荐指数
1
解决办法
729
查看次数

刷新boost :: asio中的所有异步处理程序

我正在运行一些需要异步通信的测试,底层框架是Asio.有时,即使测试已经被删除,处理程序也会保留在处理循环中,这是有充分理由的.但是,删除目标调用它.

Test类:

virtual void SetUp()
{
  _client = new Client;
  _server = new Server;

  Service::run();
}

virtual void TearDown()
{
  Service::stop();

  delete _client;
  delete _server;
}
Run Code Online (Sandbox Code Playgroud)

Service类:

static void run()
{
  _thread = new asio::thread(boost::bind(&asio::io_service::run, _service));
}

static void stop()
{
  _service->stop();

  _thread->join();
  delete _thread;

  _service->reset();
}
Run Code Online (Sandbox Code Playgroud)

io_service::stop()是非阻塞的,所以在我的情况下它变得毫无用处.如果我删除io_service函数末尾的对象,将不会调用处理程序,但我想要一个更好的解决方案,删除对象之前强制完成.

注意:实际的处理循环是在第二个线程中完成的,但它是在一个io_service::stop()包装器中连接的,整个问题似乎与线程无关.

我正在使用Asio(非Boost)1.4.5,但可以考虑升级(以获得io_service::stopped()操作?).

编辑:io_service根据评论添加包装代码,因为它似乎是相关的.

c++ boost asynchronous boost-asio

3
推荐指数
1
解决办法
4884
查看次数

为什么我在使用shared_ptr时会遇到此运行时异常?

在下面的代码中,我在返回1后得到以下运行时异常(可能是内存泄漏); 在Node()的析构函数中.

Unhandled exception at 0x0f9bad4a (msvcp100d.dll) in test.exe: 0xC0000005: Access violation reading location 0xfeeefef2.

我使用smart_ptr已经有一段时间了,所以我想知道我在这里做错了什么?

#include <vector>
#include <queue>
#include <memory>

#include <iostream>
using namespace std;

class Node;
typedef shared_ptr<Node> SharedNode;

class Node {
    Node* parent;
    vector< SharedNode > children;
    int value;

    //limiting construction
    Node(int a_value):value(a_value),parent(0){}
    Node(const Node &copy); //non-construction-copyable
    Node& operator=(const Node& copy); //non-copyable
public:
    static SharedNode create(int a_value){
        return SharedNode(new Node(a_value));
    }
    SharedNode addChild(SharedNode child){
        child->parent = this;
        children.push_back(child);
        return child;
    }

SharedNode getNode(int searchValue);
};

SharedNode …
Run Code Online (Sandbox Code Playgroud)

c++ shared-ptr

3
推荐指数
1
解决办法
4684
查看次数

如何将shared_ptr返回给拥有它的对象?

这里的类似问题似乎都使用了boost,我没有使用它.

我想要做的是通过以下方式证明:

在"所有者"中:

std::shared_ptr<State> m_state;

m_state = make_shared<State>(param);

m_state = m_state->SomeVirtualFunction();    // The original m_state object gets destroyed
Run Code Online (Sandbox Code Playgroud)

在"拥有":

std::shared_ptr<State> State::SomeVirtualFunction() {
    return std:shared_ptr<State>(this);
}
Run Code Online (Sandbox Code Playgroud)

在MSVS 2012中的Visual C++中,拥有的对象被破坏.我该如何保持活力?

c++ smart-pointers c++11

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

为什么我们需要enabled_shared_from_this

我正在研究shared_ptr和enable_shared_from_this的boost文档,我无法弄清楚enable_shared_from_this的实际用途.

以下是我对enable_shared_from_this的理解(示例复制自`enable_shared_from_this`的有用性是什么?).

class Y: public enable_shared_from_this<Y>
{
    public:
      Y(): count(3){}
      shared_ptr<Y> f()
      {
         return shared_from_this();
      }
      int count;
}

int main()
{
    shared_ptr<Y> p(new Y);
    shared_ptr<Y> q = p->f();
    cout << "\n Count from P is " << p1->count;
    cout << "\n Count from q is " << q1->count;
}
Run Code Online (Sandbox Code Playgroud)

所以,现在我们有一个共享指针q,它指向p(new Y)所拥有的同一个对象,当p和q超出范围时,对象被销毁.上面的两个打印语句都将打印3作为计数.

现在,通过执行以下操作,我可以在不使用enable_shared_from_this的情况下实现相同的功能.

class Y
{
    public:
      Y(): count(3){}
      int count;
}

int main()
{
    shared_ptr<Y> p(new Y);
    shared_ptr<Y> q(p);
    cout << "\n Count from P is " …
Run Code Online (Sandbox Code Playgroud)

c++ boost shared-ptr

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