wxEVT_LISTBOX fires when wxDialog is opened Topic is solved

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
TheGreatRambler
Knows some wx things
Knows some wx things
Posts: 29
Joined: Mon Feb 10, 2020 3:09 am

wxEVT_LISTBOX fires when wxDialog is opened

Post by TheGreatRambler » Tue Apr 07, 2020 2:02 am

Back again for more help, heh :D! It seems to be similar to this viewtopic.php?t=40750. Only on wxGTK, wxEVT_LISTBOX appears to fire when ShowModal is used on a wxDialog. This code works as intended on Windows. Here's the stacktrace:

Code: Select all

#0  0x00005555555ad1b6 in ProjectHandler::onClickProject(wxCommandEvent&) (this=0x555555b3bc60, event=...) at source/dataHandling/projectHandler.cpp:53
#1  0x00007ffff7b289ae in wxEvtHandler::ProcessEventIfMatchesId(wxEventTableEntryBase const&, wxEvtHandler*, wxEvent&) () at /usr/lib/x86_64-linux-gnu/libwx_baseu_unofficial-3.1.so.3
#2  0x00007ffff7b28e45 in wxEvtHandler::SearchDynamicEventTable(wxEvent&) () at /usr/lib/x86_64-linux-gnu/libwx_baseu_unofficial-3.1.so.3
#3  0x00007ffff7b2918f in wxEvtHandler::TryHereOnly(wxEvent&) () at /usr/lib/x86_64-linux-gnu/libwx_baseu_unofficial-3.1.so.3
#4  0x00007ffff7b2922a in wxEvtHandler::ProcessEventLocally(wxEvent&) () at /usr/lib/x86_64-linux-gnu/libwx_baseu_unofficial-3.1.so.3
#5  0x00007ffff7b292b5 in wxEvtHandler::ProcessEvent(wxEvent&) () at /usr/lib/x86_64-linux-gnu/libwx_baseu_unofficial-3.1.so.3
#6  0x00007ffff7b2ad17 in wxEvtHandler::SafelyProcessEvent(wxEvent&) () at /usr/lib/x86_64-linux-gnu/libwx_baseu_unofficial-3.1.so.3
#7  0x00007ffff72e03d1 in wxListBoxBase::SendEvent(int, int, bool) () at /usr/lib/x86_64-linux-gnu/libwx_gtk2u_unofficial_core-3.1.so.3
#8  0x00007ffff3faf10d in g_closure_invoke () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#9  0x00007ffff3fc1bf1 in  () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#10 0x00007ffff3fca715 in g_signal_emit_valist () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#11 0x00007ffff3fcb12f in g_signal_emit () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#12 0x00007ffff5622b0f in  () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#13 0x00007ffff5626455 in  () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#14 0x00007ffff3faf10d in g_closure_invoke () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#15 0x00007ffff3fc1de8 in  () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#16 0x00007ffff3fca715 in g_signal_emit_valist () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#17 0x00007ffff3fcb12f in g_signal_emit () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#18 0x00007ffff56412ca in gtk_widget_grab_focus () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#19 0x00007ffff56222f0 in  () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#20 0x00007ffff552a44b in  () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#21 0x00007ffff3faf021 in g_closure_invoke () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#22 0x00007ffff3fc1de8 in  () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#23 0x00007ffff3fca0af in g_signal_emit_valist () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#24 0x00007ffff3fcb12f in g_signal_emit () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#25 0x00007ffff5641c22 in gtk_widget_child_focus () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#26 0x00007ffff5589076 in  () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#27 0x00007ffff552a44b in  () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#28 0x00007ffff3faf021 in g_closure_invoke () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#29 0x00007ffff3fc1de8 in  () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#30 0x00007ffff3fca0af in g_signal_emit_valist () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#31 0x00007ffff3fcb12f in g_signal_emit () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#32 0x00007ffff5641c22 in gtk_widget_child_focus () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#33 0x00007ffff54b010a in  () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#34 0x00007ffff552a44b in  () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#35 0x00007ffff3faf021 in g_closure_invoke () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#36 0x00007ffff3fc1de8 in  () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#37 0x00007ffff3fca0af in g_signal_emit_valist () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#38 0x00007ffff3fcb12f in g_signal_emit () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#39 0x00007ffff5641c22 in gtk_widget_child_focus () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#40 0x00007ffff54b010a in  () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#41 0x00007ffff552a44b in  () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#42 0x00007ffff3faf021 in g_closure_invoke () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#43 0x00007ffff3fc1de8 in  () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#44 0x00007ffff3fca0af in g_signal_emit_valist () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#45 0x00007ffff3fcb12f in g_signal_emit () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#46 0x00007ffff5641c22 in gtk_widget_child_focus () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#47 0x00007ffff5650b0e in  () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#48 0x00007ffff552a44b in  () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#49 0x00007ffff3faf10d in g_closure_invoke () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#50 0x00007ffff3fc1de8 in  () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#51 0x00007ffff3fca0af in g_signal_emit_valist () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#52 0x00007ffff3fcb12f in g_signal_emit () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#53 0x00007ffff5641c22 in gtk_widget_child_focus () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#54 0x00007ffff56508e9 in  () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#55 0x00007ffff5650a53 in  () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#56 0x00007ffff3faf10d in g_closure_invoke () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#57 0x00007ffff3fc212e in  () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#58 0x00007ffff3fca715 in g_signal_emit_valist () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#59 0x00007ffff3fcb12f in g_signal_emit () at /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#60 0x00007ffff5645b96 in gtk_widget_show () at /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#61 0x00007ffff7150bc5 in wxWindow::Show(bool) () at /usr/lib/x86_64-linux-gnu/libwx_gtk2u_unofficial_core-3.1.so.3
#62 0x00007ffff713f7b4 in wxTopLevelWindowGTK::Show(bool) () at /usr/lib/x86_64-linux-gnu/libwx_gtk2u_unofficial_core-3.1.so.3
#63 0x00007ffff71976b8 in wxDialog::Show(bool) () at /usr/lib/x86_64-linux-gnu/libwx_gtk2u_unofficial_core-3.1.so.3
#64 0x00007ffff71978a0 in wxDialog::ShowModal() () at /usr/lib/x86_64-linux-gnu/libwx_gtk2u_unofficial_core-3.1.so.3
#65 0x000055555557d8f4 in MainWindow::onStart() (this=0x5555558afd30) at source/ui/mainWindow.cpp:87
#66 0x00005555555cb952 in MainApp::OnInit() (this=0x555555860e60) at source/main.cpp:20
#67 0x00005555555cb7e5 in wxAppConsoleBase::CallOnInit() (this=<optimized out>) at /usr/include/wx-3.1-unofficial/wx/app.h:93
#68 0x00007ffff7a33902 in wxEntry(int&, wchar_t**) () at /usr/lib/x86_64-linux-gnu/libwx_baseu_unofficial-3.1.so.3
#69 0x00005555555cb7a6 in main(int, char**) (argc=<optimized out>, argv=<optimized out>) at source/main.cpp:47
I used this to bind the event:

Code: Select all

projectList->Bind(wxEVT_LISTBOX, &ProjectHandler::onClickProject, this);

User avatar
doublemax
Moderator
Moderator
Posts: 14992
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxEVT_LISTBOX fires when wxDialog is opened

Post by doublemax » Tue Apr 07, 2020 5:58 am

I'm not sure if you could consider this a bug (in wxWidgets). Why is the generation of this event a problem? If your application is not ready to handle that event, because it's not fully initialized, move the Bind() call to after the initialization.
Use the source, Luke!

TheGreatRambler
Knows some wx things
Knows some wx things
Posts: 29
Joined: Mon Feb 10, 2020 3:09 am

Re: wxEVT_LISTBOX fires when wxDialog is opened

Post by TheGreatRambler » Tue Apr 07, 2020 10:41 pm

How could I implement that? When binding, I expect that the event will fire once the user interacts with the widget. It is this way with windows, but it seems to not be the case in this specific circumstance.

TheGreatRambler
Knows some wx things
Knows some wx things
Posts: 29
Joined: Mon Feb 10, 2020 3:09 am

Re: wxEVT_LISTBOX fires when wxDialog is opened

Post by TheGreatRambler » Wed Apr 08, 2020 2:04 am

I assume you mean I should not bind events in the constructor of the wxDialog in question?

User avatar
doublemax
Moderator
Moderator
Posts: 14992
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxEVT_LISTBOX fires when wxDialog is opened

Post by doublemax » Wed Apr 08, 2020 6:16 am

TheGreatRambler wrote:
Wed Apr 08, 2020 2:04 am
I assume you mean I should not bind events in the constructor of the wxDialog in question?
No, in general that's ok. Suppose there is a certain member variable that is used in the event handler. But at the time you construct the wxListBox, it's not initialized yet. Then the Bind() call should be after that member variable is initialized.

But we still haven't confirmed if that's the actual problem in your case. It's just a common problem/mistake.

E.g. does it still crash if you comment out the Bind() call? If yes, the problem lies somewhere else.
Use the source, Luke!

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4264
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxEVT_LISTBOX fires when wxDialog is opened

Post by ONEEYEMAN » Wed Apr 08, 2020 3:04 pm

Hi,
Please describe what is the problem you are facing.

It is not clear from the backtrace.

The wxListBox control behave exactly the same on all 3 major platforms.

Are you saying that:

1. You have wxDialog.
2. You call ShowModal on this dialog.
3. You receive an event.

Thank you.

TheGreatRambler
Knows some wx things
Knows some wx things
Posts: 29
Joined: Mon Feb 10, 2020 3:09 am

Re: wxEVT_LISTBOX fires when wxDialog is opened

Post by TheGreatRambler » Thu Apr 09, 2020 4:58 am

I need to learn to be more clear with these forum posts! The exception in question occurs because the list box is opened much too soon and with data that is unexpected at the opening of the wxDialog, along with a suspected bug on my side. The point remains, however, that I am not exactly certain why is being called so soon. Here is the constructor:

Code: Select all

ProjectHandler::ProjectHandler(DataProcessing* dataProcessingInstance, rapidjson::Document* settings)
	: wxDialog(NULL, wxID_ANY, "Select Project", wxDefaultPosition, wxDefaultSize) {
	// ...
	projectList = new wxListBox(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, NULL, wxLB_SINGLE);
	// ...
	projectList->InsertItems(listboxSize, listboxItems, 0);
	mainSizer->Add(projectList, 0, wxEXPAND | wxALL, 5);
	projectList->Bind(wxEVT_LISTBOX, &ProjectHandler::onClickProject, this);
	
	SetSizer(mainSizer);
	mainSizer->SetSizeHints(this);
	Layout();
	Fit();
	Center(wxBOTH);

	Layout();
}
I don't have any events bound to window initialization or open, but if I should bind the event there, then I would. Also:
Are you saying that:

1. You have wxDialog.
2. You call ShowModal on this dialog.
3. You receive an event.
That's exactly what is happening/ Even before the dialog becomes visible, it is called. I'm not certain why this is not the case on Windows. And, the bind call is at the very end of the constructor, as shown above, so all member variables are initialized at the time of the bind.

User avatar
doublemax
Moderator
Moderator
Posts: 14992
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxEVT_LISTBOX fires when wxDialog is opened

Post by doublemax » Thu Apr 09, 2020 9:13 am

Even before the dialog becomes visible, it is called. I'm not certain why this is not the case on Windows.
This might just be unique to the native control under GTK. I wouldn't worry about that and instead make your code safe to handle this situation.

Please show the body for ProjectHandler::onClickProject.
Use the source, Luke!

TheGreatRambler
Knows some wx things
Knows some wx things
Posts: 29
Joined: Mon Feb 10, 2020 3:09 am

Re: wxEVT_LISTBOX fires when wxDialog is opened

Post by TheGreatRambler » Fri Apr 10, 2020 1:44 am

Code: Select all

void ProjectHandler::onClickProject(wxCommandEvent& event) {
	rapidjson::GenericArray<false, rapidjson::Value> recentProjectsArray = (*mainSettings)["recentProjects"].GetArray();
	int selectedProject                                                  = event.GetInt();
	if(selectedProject == recentProjectsArray.Size() - 1) {
		// Open up a load project dialog
		wxDirDialog dlg(NULL, "Choose Project Directory", "", wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST);
		if(dlg.ShowModal() == wxID_OK) {
			projectDir.Open(dlg.GetPath());
			recentProjectChoice = -1;
			projectName         = "Unnamed";
			projectChosen       = true;

			loadProject();
			Close(true);
		}
	} else if(selectedProject == recentProjectsArray.Size()) {
		// Open up a new project dialog
		wxDirDialog dlg(NULL, "Choose Project Directory", "", wxDD_DEFAULT_STYLE);
		if(dlg.ShowModal() == wxID_OK) {
			projectDir.Open(dlg.GetPath());
			recentProjectChoice = -1;
			projectName         = "Unnamed";
			projectChosen       = true;

			Close(true);
		}
	} else {
		// It's a project folder
		projectDir.Open(recentProjectsArray[selectedProject]["projectDirectory"].GetString());
		recentProjectChoice = selectedProject;
		projectName         = std::string(recentProjectsArray[selectedProject]["projectName"].GetString());
		projectChosen       = true;

		loadProject();
		Close(true);
	}
}
This is the unabridged version as of now. Because it always seems to select the first item, which how my program is set up may attempt to load a non-existent folder from disk, which errors. Upon selection, the frame closes itself, so you can't reverse your decision in this case. I wouldn't want to change this if I didn't have to, should I try testing for GTK and disabling the first selection in that case?

User avatar
doublemax
Moderator
Moderator
Posts: 14992
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxEVT_LISTBOX fires when wxDialog is opened

Post by doublemax » Fri Apr 10, 2020 8:39 am

...should I try testing for GTK and disabling the first selection in that case?
I guess you have to this in this case. But this still doesn't explain why and where the code crashes.

Code: Select all

ProjectHandler::onClickProject(wxCommandEvent&) (this=0x555555b3bc60, event=...) at source/dataHandling/projectHandler.cpp:53
I assume 53 is the code line? Which line is it?

This should all be easy to solve by using a debugger.
Use the source, Luke!

TheGreatRambler
Knows some wx things
Knows some wx things
Posts: 29
Joined: Mon Feb 10, 2020 3:09 am

Re: wxEVT_LISTBOX fires when wxDialog is opened

Post by TheGreatRambler » Fri Apr 10, 2020 8:44 pm

I've fixed the code. I just added a Gtk specific hack that detected when the listbox was first selected and deselect it then. I find it interesting that gtk automatically selects the first item, but I've managed to get around that.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4264
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxEVT_LISTBOX fires when wxDialog is opened

Post by ONEEYEMAN » Fri Apr 10, 2020 10:02 pm

Hi,
It would be nice to know if this can be seen in the widgets sample.
Also, what is the exact GTK+ version?

Thank you.

TheGreatRambler
Knows some wx things
Knows some wx things
Posts: 29
Joined: Mon Feb 10, 2020 3:09 am

Re: wxEVT_LISTBOX fires when wxDialog is opened

Post by TheGreatRambler » Sat Apr 11, 2020 2:33 am

I believe I am using GTK+2, though I think I'll change that now that I have figured that out. Is there a place where I can simply download the samples? This computer isn't powerful enough to compile the wxWidgets source code and the wx3.1-samples package doesn't have the executable itself.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4264
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxEVT_LISTBOX fires when wxDialog is opened

Post by ONEEYEMAN » Sat Apr 11, 2020 3:22 am

Hi,
Just clone the repository somewhere...

Thank you.

TheGreatRambler
Knows some wx things
Knows some wx things
Posts: 29
Joined: Mon Feb 10, 2020 3:09 am

Re: wxEVT_LISTBOX fires when wxDialog is opened

Post by TheGreatRambler » Sat Apr 11, 2020 4:34 am

:evil: I hate platform differences. Switched to the GTK+3 based wxWidgets and it solved that and a lot of other problems I have been having. Thank you so much for your help, you guys are the best for helping noobs like me! I should really compile source next time I get on here...

Post Reply