David Brown
David Brown

Reputation: 36269

Access violation when calling method of base class

I'm fairly new to C++, so I'm probably missing something obvious. I'll admit that I don't fully understand how the object model works, but I hope I can learn something from this problem...

I have a simple wxWidgets application with a custom wxFrame class. It hooks up an event and inside that event handler, I want to set the title of the frame. When the event handler executes, however, the debugger shows that the address of the SetTitle function is 0x00000000 and I get an access violation exception.

class BrowserFrame : public wxFrame {
public:
    BrowserFrame();

    void OnChangeTitle(AweChangeTitleEvent& evt);

private:
    AweWebView* m_webView;
};

BrowserFrame::BrowserFrame() : wxFrame(NULL, wxID_ANY, wxGetApp().Name) {
    m_webView = new AweWebView(this, wxID_ANY);
    m_webView->Connect(aweEVT_CHANGE_TITLE, AweChangeTitleEventHandler(BrowserFrame::OnChangeTitle));
    m_webView->WebView()->LoadURL(Awesomium::WebURL(Awesomium::WSLit("http://www.google.com")));
}

void BrowserFrame::OnChangeTitle(AweChangeTitleEvent& evt) {
    SetTitle(evt.GetTitle()); // SetTitle == 0x00000000, Access violation
}

My first thought was that the BrowserFrame was getting destructed somehow. That doesn't seem to be the case, though, because I can access it with GetEventObject(). This works just fine, for example:

void BrowserFrame::OnChangeTitle(AweChangeTitleEvent& evt) {
    static_cast<wxFrame*>(static_cast<wxWindow*>(evt.GetEventObject())->GetParent())->SetTitle(evt.GetTitle());
}

I can't think of anything I've written that would corrupt the v-table in any way, except for one instance where I do my own memory management on an image buffer. I've completely removed that portion and still get the access violation on SetTitle, so I don't think that's the source of the problem.

What am I missing?

Upvotes: 1

Views: 628

Answers (1)

David Brown
David Brown

Reputation: 36269

I managed to fix it. I'll try to explain the solution as best I can.

If I understand the problem correctly, the event handler wasn't actually associated with an instance of BrowserFrame. I switched to the more modern Bind<>() method of connecting wxWidgets events and passed it the instance of BrowserFrame:

m_webView->Bind(aweEVT_CHANGE_TITLE, &BrowserFrame::OnChangeTitle, this);

I also had to update my custom event declarations to make them compatible with Bind<>(), but after that, SetTitle() is called and everything works great.

Upvotes: 0

Related Questions