Dynamic Event Handling - Read access violation

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
itachiboi999
Knows some wx things
Knows some wx things
Posts: 33
Joined: Mon Nov 15, 2021 7:06 pm

Dynamic Event Handling - Read access violation

Post by itachiboi999 »

Code in question:

Code: Select all

buttonGrid = new wxButton * [nFieldWidth * nFieldHeight];
wxGridSizer* grid = new wxGridSizer(nFieldWidth, nFieldHeight, 0, 0);

// for the specified number of buttons, loop thru and build grid of buttons
for (int x = 0; x < nFieldWidth; x++) {
	for (int y = 0; y < nFieldHeight; y++) {
		buttonGrid[y * nFieldWidth + x] = new wxButton(panel_center, 10010 + (y * nFieldWidth + x));
		grid->Add(buttonGrid[y*nFieldWidth + x], 1, wxEXPAND | wxALL);

		//dynamically bind buttons to event handler
		buttonGrid[y*nFieldWidth + 1]->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &cppMain::OnGridClick, this);
	}
}

//tell parent which sizer to use
panel_center->SetSizerAndFit(grid);
//adjust layout with new given information
grid->Layout();
The error message:
Exception thrown: read access violation.
this was 0xCDCDCDCD.
From the error message I can see that "this" is not initialized (due to the read value being 0xCDCDCDCD); But I am honestly a little unsure of what "this" is referencing
i thought that it was referring to the button that originally generated this event, but since i have that initialized in my header file as

Code: Select all

int nFieldWidth = 10;
int nFieldHeight = 5;
wxButton** buttonGrid = nullptr; //array of button pointers
i figured that this couldn't be the case.... and so i am currently stumped at the moment.
I have referred to https://docs.wxwidgets.org/trunk/overvi ... vents_bind for help and read that:
MyFrame::MyFrame(...)
{
m_child->Bind(wxEVT_LEAVE_WINDOW, &MyFrame::OnMouseLeave, this);
}
will work exactly as expected. Note that you can get the object that generated the event – and that is not the same as the frame – via wxEvent::GetEventObject() method of event argument passed to the event handler.
which gives me hope for this method of dynamic event binding, but i just think i'm a little tied up with/confused on what needs to be used as the argument for what my frame handler should be..

{all help is appreciated, pls & thank you}
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7449
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Dynamic Event Handling - Read access violation

Post by ONEEYEMAN »

Hi,
First things first: what is the backtrace at the time of the crash?
Second: the code you posted - where is it from? Is it somewhere in the cppMain: class?

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Dynamic Event Handling - Read access violation

Post by doublemax »

Code: Select all

buttonGrid[y*nFieldWidth + 1]->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &cppMain::OnGridClick, this);
The index should probably be "y * nFieldWidth + x" here.
Use the source, Luke!
Post Reply