title: Inherited class "invalid pointer error" when calling virtual functions
body: <p>As you can see in the code below, I have an Abstract Base Class "HostWindow", and class that derives from it "Chrome". All the functions are implemented in Chrome. The issue is, I can't call functions in Chrome if they're virtual.</p>
  
  <pre><code>class HostWindow : public Noncopyable {
  public:
      virtual ~HostWindow() { }
  
      // Pure virtual functions:
      virtual void repaint(const IntRect&amp;, bool contentChanged, bool immediate = false, bool repaintContentOnly = false) = 0;
      virtual void scrollbarsModeDidChange() const = 0;
  }
  
  class Chrome : public HostWindow {
      // HostWindow functions:
      virtual void repaint(const IntRect&amp;, bool contentChanged, bool immediate = false, bool repaintContentOnly = false);
      virtual void scrollbarsModeDidChange() const;
  
      void focus() const;
  }
  </code></pre>
  
  <p>So lets say we have an instance of Chrome, and we call a few functions:</p>
  
  <pre><code>WebCore::Chrome *chrome = new Chrome();
  chrome-&gt;repaint(IntRect(), true); // Null pointer error
  chrome-&gt;focus(); // returns void (works)
  </code></pre>
  
  <p>The null pointer error I get whenever I call virtual functions is:</p>
  
  <blockquote>
    <p>Program received signal EXC_BAD_ACCESS, Could not access memory.
    Reason: KERN_PROTECTION_FAILURE at address: 0x00000008</p>
  </blockquote>
  
  <p>Any idea what's happening?</p>
  
  <p><strong>Update:</strong>
  As many of you pointed out - this code actually runs. Unfortunately I can't provide a more full example, since the code is deep inside WebCore (WebKit). However, I have narrowed the problem down. If I create a Chrome instance manually, calling virtual functions work. So the issue is with this particular chrome instance - it can't instantiated properly. Now, the Chrome instance is instantiated in a constructor of another class. I'll investigate further...</p>
  
  <p><strong>Update 2:</strong>
  Ok, examining the vtable on the offending instance shows that it's null; from GDB:</p>
  
  <pre><code>p *(void **)chrome
  $52 = (void *) 0x0
  </code></pre>
  
  <p>A normal instance has a correct vtable. So, I've got to work out why the vtable is nil - I wonder how that could happen? Maybe because it's being instantiated in some other classes Constructor?</p>
  
  <p><strong>Update 3:</strong>
  Looks like I'm correct about the issue being it's instantiation inside another class' constructor. </p>
  
  <p>So, before the instantiation looked like this:</p>
  
  <pre><code>Page::Page(ChromeClient* chromeClient, ...)
      : m_chrome(new Chrome(this, chromeClient))
  </code></pre>
  
  <p>And m_chrome is an invalid instance, with a nil vtable.
  I've changed the instantiation so it happens when the first time the variable is needed (this involves saving ChromeClient for later):</p>
  
  <pre><code>Page::Page(ChromeClient* chromeClient, ...)
      : m_chrome(0)
      , m_chrome_client(chromeClient)
  
  Chrome* Page::chrome() const {
    if(!m_chrome) {
      m_chrome = new Chrome(this, m_chrome_client);
    }
    return m_chrome;
  }
  </code></pre>
  
  <p>Now the Page::chrome() instance is the correct one, with the proper vtable - rather odd!</p>
  
  <p><strong>Update 4:</strong>
  Last update, I promise :). Ok, so I've pinpointed it down exactly. You get the correct instance, with the vtable, if you instantiate it in Page constructor's body. If you instantiate it in Page constructor's head, it doesn't have a vtable. Is there any limitation in the types of variable setting you can do in a constructor's head? I guess that's another Stackoverflow question.</p>
  
  <p>Thanks guys for being so helpful.</p>