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.
class HostWindow : public Noncopyable {
public:
virtual ~HostWindow() { }
// Pure virtual functions:
virtual void repaint(const IntRect&, 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&, bool contentChanged, bool immediate = false, bool repaintContentOnly = false);
virtual void scrollbarsModeDidChange() const;
void focus() const;
}
So lets say we have an instance of Chrome, and we call a few functions:
WebCore::Chrome *chrome = new Chrome();
chrome->repaint(IntRect(), true); // Null pointer error
chrome->focus(); // returns void (works)
The null pointer error I get whenever I call virtual functions is:
Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_PROTECTION_FAILURE at address: 0x00000008
Any idea what's happening?
Update: 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...
Update 2: Ok, examining the vtable on the offending instance shows that it's null; from GDB:
p *(void **)chrome
$52 = (void *) 0x0
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?
Update 3: Looks like I'm correct about the issue being it's instantiation inside another class' constructor.
So, before the instantiation looked like this:
Page::Page(ChromeClient* chromeClient, ...)
: m_chrome(new Chrome(this, chromeClient))
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):
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;
}
Now the Page::chrome() instance is the correct one, with the proper vtable - rather odd!
Update 4: 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.
Thanks guys for being so helpful.