views:

278

answers:

5

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.

+2  A: 

Yes, the 'this' pointer is zero. Add 8 to get an offset, and there's your fault. You apparently don't have any actual object at all.

Since you haven't posted enough code to really come to grips, I'm guessing. Either the entire this pointer is 0, or the virtual function table pointer is 0, perhaps because the object has been deleted after it was created and before you try to call it.

The best advice I can give you is to create a much smaller test-tube. Either you will find your problem or you will end up with a postable example.

The vtbl isn't in place in an instance until the end of the construction process. In fact, the spec requires progressive modification of the vtbl to match the state of construction of the class hierarchy.

bmargulies
Could you elaborate? I'm fairly new to C++.I do have an instance, Chrome, and I can call some functions - like focus(), but not ones defined in the ABC, like repaint().
Alex MacCaw
'Chrome' isn't the instance, it's the class. 'chrome' is the instance.
John Dibling
The vtable is nil - see Updates. I've no idea how it could be happening.
Alex MacCaw
You deleted it after creating it and before using it is the usual method.
bmargulies
A: 

Can you post the complete code?

After slight modification in your code(whatever is available), it works :

#include <iostream>

class HostWindow  {
public:
    virtual ~HostWindow() { }

    // Pure virtual functions:
    virtual void repaint(const int , bool contentChanged, bool immediate = false, bool repaintContentOnly = false) = 0;
    virtual void scrollbarsModeDidChange() const = 0;
};

class Chrome : public HostWindow {
public:
    // HostWindow functions:
    virtual void repaint(const int , bool contentChanged, bool immediate = false, bool repaintContentOnly = false) 
    {
        std::cout << "In repaint." << std::endl;
    }
    virtual void scrollbarsModeDidChange() const { }

    void focus() const
    {
        std::cout << "In focus." << std::endl;
    }
};

int main()
{
    Chrome *chrome = new Chrome();
    chrome->repaint(1, true); // Null pointer error
    chrome->focus();
    delete chrome;
    return 0;
}
sand
A: 

I'm not familiar with the code base you have, but shouldn't you write the following:

// note the 'WebCore::Chrome()'
WebCore::Chrome *chrome = new WebCore::Chrome();
chrome->repaint(IntRect(), true); // 'chrome' should be a valid pointer now

instead of:

WebCore::Chrome *chrome = new Chrome();
chrome->repaint(IntRect(), true); // Null pointer error
Dmitry
Yes, that was a typo in the example.
Alex MacCaw
@Alex: That is why you should always cut/paste code. __NEVER__ retype as it just adds errors and results in a lot of busy null work for people helping you.
Martin York
A: 

ssume your non-copyable are as following (at least for mine did)

class NonCopyable
{
protected:
    NonCopyable() {}
    ~NonCopyable() {}
private:
    NonCopyable( const NonCopyable& );
    const NonCopyable& operator=( const NonCopyable& );
};

after inserting public modifier to class chrome's function and some dummy implementation for them, the whole thing worked without stated problem.

there are no problem with the code posted, it might be you are doing things wrong and not posting those part here.

lastly, DO checking for allocation failure. (yes, "new" are allocation on heap)

YeenFei
Did you read the updates?
Alex MacCaw
unfortunately, i did...what is class Page ?u added code snippet without details for other people to understand
YeenFei
A: 

I found that this was being caused by allowing all symbols to be exported.

Usually, WebCore only has a subset of symbols exported - basically on things that WebKit needs.

I changed that to export every symbol - and it somehow caused this error.

Alex MacCaw