title: Why does accessing a data member of a forward declared class give me a read access violation?
body: <p>I have two classes, in the first class, 'DrawGraphics' I am trying to access a member variable from a class that I have forward declared 'MainComponent'. However, this leads to a read access violation. </p>
  
  <p>This is the error message:</p>
  
  <pre><code>Unhandled exception thrown: read access violation.
  std::_Unique_ptr_base&lt;juce::Slider::Pimpl,std::default_delete&lt;juce::Slider::Pimpl&gt; &gt;::_Myptr(...) returned 0xFFFFFFFFFFFFFFFF.
  </code></pre>
  
  <p>I'm using the JUCE framework by the way, the exception occurs here:</p>
  
  <pre><code>    _NODISCARD pointer operator-&gt;() const _NOEXCEPT
          {   // return pointer to class object
          return (this-&gt;_Myptr()); // &lt;-- this is the exception breakpoint
          }
  </code></pre>
  
  <p>what you see below is only about 20% of the actual code but I'm going to try and only include the relevant parts of each file so as not to drown you in questionably written code. </p>
  
  <p><strong>DrawGraphics.h</strong></p>
  
  <pre><code>class MainComponent;
  
  class DrawGraphics : public Component
  {
  public :
  
  void clock();
  
  private:
  
      MainComponent* mainComponent;
  };
  </code></pre>
  
  <p><strong>DrawGraphics.cpp</strong></p>
  
  <pre><code>#include "DrawGraphics.h"
  #include "MainComponent.h"
  
  void DrawGraphics::clock()
  {
  
      double sliderOutput = mainComponent-&gt;ampSlider.getValue();
  
      // THIS ^ CAUSES THE ERROR
  
      DBG("Slider output is : " &lt;&lt; sliderOutput);
  
  }
  </code></pre>
  
  <p><strong>MainComponent.h</strong></p>
  
  <pre><code>class MainComponent : public Component,
                        public Slider::Listener
  {
  public:
      Slider ampSlider;
      void sliderValueChanged(Slider* slider) override;
  
  private:
      DrawGraphics drawGraphics;
  };
  </code></pre>
  
  <p><strong>MainComponent.cpp</strong></p>
  
  <pre><code>void MainComponent::sliderDragStarted(Slider* slider)
  {
      if (slider == &amp;ampSlider)
      {
          drawGraphics.clock();
      }
  }
  </code></pre>
  
  <p>I was hoping to simply be able to access the data stream that is produced from ampSlider. Apologies in advance if I failed to include parts of the code that are actually relevant to the issue.</p>
  
  <p>...</p>
  
  <p>EDIT:
  Ok so my problem was I hadn't intialised mainComponent. I did this by changing
  <code>MainComponent* mainComponent;</code> to <code>MainComponent* mainComponent{};</code> in DrawGraphics.h</p>
  
  <p>and then in DrawGraphics.cpp I created an object of mainComponent by simply adding <code>mainComponent = new MainComponent;</code> at the start of DrawGraphics::clock()</p>
  
  <p>It seemed as though when trying to get a value of ampSlider, I was referering to an empty, unitialised object.</p>
  
  <p>Thanks for all the help. </p>