title: read access violation when assigning to std::function
body: <p>I'm trying to pass a callback to a member in one of my classes. The member is held in a shared_ptr because class A is not the only owner. </p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;functional&gt;
  #include &lt;memory&gt;
  
  class B {
  public:
      void update() {
          cb(5);
      }
      void set_callback(std::function&lt;void(int)&gt; callback) {
          cb = callback; // exception thrown here
      }
  private:
      std::function&lt;void(int)&gt; cb;
  };
  
  class A {
  public:
      A() : b() {
          b-&gt;set_callback([](int x) {
              std::cout &lt;&lt; x &lt;&lt; std::endl;
          });
      }
      void update() {
          b-&gt;update();
      }
  private:
      std::shared_ptr&lt;B&gt; b; // seemingly the culprit
  };
  
  int main()
  {
      A a;
      a.update();
  }
  </code></pre>
  
  <p>This program should print <code>5</code>, but I get an read access violation from inside the "functional" STL header. <a href="https://imgur.com/a/tAu4DpP" rel="nofollow noreferrer">Here are screenshots of the exception and call stack</a>. In the code, changing <code>std::shared_ptr&lt;B&gt;</code> to <code>B</code> fixes the read access violation. What am I doing wrong?</p>