Home > Enterprise >  enable share from this fails with bad_weak_ptr
enable share from this fails with bad_weak_ptr

Time:11-05

I am trying to learn weak_ptr and enable_shared_from_this, Here, I want Component to have pointer to Mediator and Mediator to know about Component. I don't understand why this code fails with bad_weak_ptr. How to solve this ? I understand, for shared_from_this, this has to own by shared_ptr, which I think is true here, still it fails, reasons ?

#include <iostream>
#include <string>
#include <memory>


using namespace std;
class BaseComponent;

class Mediator: public enable_shared_from_this<Mediator> {
 public:
  virtual void Notify(std::string event) const = 0;
};

/**
 * The Base Component provides the basic functionality of storing a mediator's
 * instance inside component objects.
 */
class BaseComponent : public enable_shared_from_this<BaseComponent> {
 protected:
  weak_ptr<Mediator> mediator_;

 public:
  BaseComponent() : mediator_() {
  }
  void set_mediator(shared_ptr<Mediator> mediator) {
    mediator_ = mediator;
  }
};

/**
 * Concrete Components implement various functionality. They don't depend on
 * other components. They also don't depend on any concrete mediator classes.
 */
class Component1 : public BaseComponent {
 public:
  void DoA() {
    std::cout << "Component 1 does A.\n";
    if(auto w_mediator_ = mediator_.lock())
      w_mediator_->Notify("A");
  }
  void DoB() {
    std::cout << "Component 1 does B.\n";
    if(auto w_mediator_ = mediator_.lock())
      w_mediator_->Notify("B");
  }
};

class Component2 : public BaseComponent {
 public:
  void DoC() {
    std::cout << "Component 2 does C.\n";
    if(auto w_mediator_ = mediator_.lock())
      w_mediator_->Notify("C");
  }
  void DoD() {
    std::cout << "Component 2 does D.\n";
    if(auto w_mediator_ = mediator_.lock())
      w_mediator_->Notify("D");
  }
};

/**
 * Concrete Mediators implement cooperative behavior by coordinating several
 * components.
 */
class ConcreteMediator : public Mediator {
 private:
  shared_ptr<Component1> component1_;
  shared_ptr<Component2> component2_;

 public:
  ConcreteMediator(shared_ptr<Component1> c1, shared_ptr<Component2> c2) : component1_(c1), component2_(c2) {
    component1_->set_mediator(shared_from_this());
    component2_->set_mediator(shared_from_this());
  }
  void Notify(std::string event) const override {
    if (event == "A") {
      std::cout << "Mediator reacts on A and triggers following operations:\n";
      component2_->DoC();
    }
    if (event == "D") {
      std::cout << "Mediator reacts on D and triggers following operations:\n";
      component1_->DoB();
      component2_->DoC();
    }
  }
};

/**
 * The client code.
 */

void ClientCode() {
  auto c1 = make_shared<Component1>();
  auto c2 = make_shared<Component2>();
  auto mediator = make_shared<ConcreteMediator>(c1, c2);
  std::cout << "Client triggers operation A.\n";
  c1->DoA();
  std::cout << "\n";
  std::cout << "Client triggers operation D.\n";
  c2->DoD();

}

int main() {
  ClientCode();
  return 0;
}

CodePudding user response:

When you call share_from_this the object must be managed by a shared_ptr.

It's not yet managed by a shared_ptr inside the constructor, but you can move that code out and put it in a separate function that you call on mediator after construction.

class ConcreteMediator : public Mediator {
public:
    ConcreteMediator(shared_ptr<Component1> c1, shared_ptr<Component2> c2)
        : component1_(c1), component2_(c2) {}

    void set_mediators() {
        component1_->set_mediator(shared_from_this());
        component2_->set_mediator(shared_from_this());        
    }
};

auto mediator = make_shared<ConcreteMediator>(c1, c2);
mediator->set_mediators(); // this will work
  • Related