strange call from event problem

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
vanarieleyen
Knows some wx things
Knows some wx things
Posts: 47
Joined: Thu Aug 29, 2019 3:55 am
Location: China, Shenzhen

strange call from event problem

Post by vanarieleyen »

I have a strange problem that is solvable but the way to solve it is not my style....

There is the main frame (previewFrame) and a class that inherits from wxArrangeCtrl.
When I select an item in the list I want to fire an event that will display the properties of the selected item.
The event fires but the result is strange.
I have removed most of the code to show what is happening:

Code: Select all


class RenderList : public wxRearrangeCtrl {
  public:
	// constructor etc .... 
	// .......

	// bind a function to the wxListctrl event
	void bindEvent(int eventType, wxObjectEventFunction onEvent) {
		int listID = list->GetId();
		Connect(listID, eventType, onEvent);
	}
}


class previewFrame: public wxFrame {
  public:
	// constructor etc .... 
	// .......
	
	RenderList * renderers;
	int count = 10;
	
	void fillProperties(int idx) ;
	void OnRendererSelect(wxCommandEvent& event);
}

// the main frame constructor
previewFrame::previewFrame(wxWindow* parent,wxWindowID id) {
         Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("wxID_ANY"));

	// create the renderlist (wxRearrangeCtrl)
        renderers = new RenderList(scrollerTop, ID_RENDERLIST, wxPoint(0,0), wxSize(350,200));

	// bind OnRendererSelect() to the listCtrl from renderers
	renderers->bindEvent(wxEVT_COMMAND_LISTBOX_SELECTED,
	                     (wxObjectEventFunction)&previewFrame::OnRendererSelect);
}

// called by onselect event from Renderlist::wxListCtrl
void previewFrame::OnRendererSelect(wxCommandEvent& event) {
	int idx = event.GetInt();		// the index of the list item that is clicked

	fillProperties(idx);			// from the previewFrame class - no problems
}

// variable a has a random value - there is no error, it is just random
void previewFrame::fillProperties(int idx) {
	int a = count;			// count from the previewFrame class (should be 10)
}

In the creation of previewFrame I also create the Renderlist and connect the event to a function that belongs to previewFrame (I think this is the problem).
The event fires and calls OnRendererSelect().
OnRendererSelect() calls fillProperties()
fillProperties assigns integer-a to count (member of previewFrame which is initialized as 10)

When I run this code in the debugger it should show that 'a' is 10. However, the value is random.

And now my temporary solution:
I make a global variable (pointer) of the type previewFrame and assign this to the previewFrame that is created.
previewFrame *t;

In the previewFrame constructor:
t = this;

Then in fillProperties():
int a = t->count;

this solves the problem but it is far from elegant.

What is going on and what is my mistake?
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: strange call from event problem

Post by doublemax »

The problem lies here:

Code: Select all

renderers->bindEvent(wxEVT_COMMAND_LISTBOX_SELECTED,
	                     (wxObjectEventFunction)&previewFrame::OnRendererSelect);
You're connecting the event handler to "renderers", that's what "this" inside the event handler OnRendererSelect will point to. You need to pass an instance of previewFrame as event sink parameter of the Connect call.

Code: Select all

renderers->bindEvent(wxEVT_COMMAND_LISTBOX_SELECTED,
	                     (wxObjectEventFunction)&previewFrame::OnRendererSelect, NULL, this);
Even better, get rid of Connect() and the ugly wxObjectEventFunction casts. Use Bind() instead which will catch problems like this at compile time.
Use the source, Luke!
vanarieleyen
Knows some wx things
Knows some wx things
Posts: 47
Joined: Thu Aug 29, 2019 3:55 am
Location: China, Shenzhen

Re: strange call from event problem

Post by vanarieleyen »

Thank you for your tip, it works perfectly now

I have changed it and also moved to Bind() instead of Connect().
Post Reply