开发者

Inherited class "invalid pointer error" when calling virtual functions

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 iss开发者_运维百科ue 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.


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.


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;
}


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


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜