Problem with frame pointer

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
MinusPL
In need of some credit
In need of some credit
Posts: 4
Joined: Sat Jul 13, 2019 12:40 pm

Problem with frame pointer

Post by MinusPL »

Hi everyone!

I'm using wxWidgets 3.1.2

I'm trying to write simple application to extract some files.
However right now I'm stuck at creating dialogs.
Dialogs need to have parent, when I choose option in menu I show normal file browser from wxWidgets, and later after choosing file It shows dialog.
In this dialog I have some "settings" for file which is going to be extracted. In this one I also have two buttons one for canceling, and other one that will close this dialog and show another one, but when I pass frame pointer as parrent for next dialog my frame is nullptr.
Do I need to close previous dialog before opening other one?
Or maybe I'm doing something wrong in my code.
Since I don't know it's simple issue with just C++ programming or with wxWidgets, I'm not adding code yet. When you need anything just ask, I'll make an answer with all code you need :)
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problem with frame pointer

Post by doublemax »

For all toplevel windows (wxDialog and wxFrame) you can pass NULL as parent.
Use the source, Luke!
MinusPL
In need of some credit
In need of some credit
Posts: 4
Joined: Sat Jul 13, 2019 12:40 pm

Re: Problem with frame pointer

Post by MinusPL »

Thanks for that information.
Still I need to have pointer to my dialog since it will have information about currently processed file.
When I want to access pointer inside my frame class it is nullptr (built-in this pointer is nullptr).
It is called in other dialog. My first dialog has command for button. It is then reading needed data from controls and sends them back to frame.
From frame I use these passed values and create second dialog.
First call to frame is succesfull since it is going to proper function and executes it. Only when I want to create another dialog this pointer is suddenly equal to nullptr (basically 0).
I wonder how it happens, since in my code there's not a single change in frame pointer.
Any ideas why and how this is happening?
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problem with frame pointer

Post by doublemax »

I'd need to see some (at least pseudo) code that shows in which order windows are created and destroyed.
Use the source, Luke!
MinusPL
In need of some credit
In need of some credit
Posts: 4
Joined: Sat Jul 13, 2019 12:40 pm

Re: Problem with frame pointer

Post by MinusPL »

No problem here you go:

Code: Select all


MyFrame::MyFrame()
	: wxFrame(NULL, wxID_ANY, c_msg.c_str())
{
	wxMenu *menuFile = new wxMenu;
	menuFile->Append(ID_Extract, "&Extract",
		"Extract SCS# Archive");
	menuFile->Append(ID_Pack, "&Pack",
		"Pack SCS# Archive");
	menuFile->AppendSeparator();
	menuFile->Append(wxID_EXIT);
	wxMenu *menuHelp = new wxMenu;
	menuHelp->Append(wxID_ABOUT);
	wxMenuBar *menuBar = new wxMenuBar;
	menuBar->Append(menuFile, "&File");
	menuBar->Append(menuHelp, "&Help");
	SetMenuBar(menuBar);
	CreateStatusBar();
	SetStatusText("SCSArchiver - the way it was supposed to be done...");
	Bind(wxEVT_MENU, &MyFrame::OnExtract, this, ID_Extract);
	Bind(wxEVT_MENU, &MyFrame::OnPack, this, ID_Pack);
	Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
	Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
}

bool MyApp::OnInit()
{
	srand(time(NULL));
	r = rand() % msg.size();
	c_msg.assign("SCSArchiver - ").append(msg[r]);
	MyFrame *frame = new MyFrame();
	//frame->exSettings = new ExtractSettings();
	frame->Show(true);
	return true;
}

void MyFrame::OnExtract(wxCommandEvent& event)
{
	wxFileDialog *fBrowser = new wxFileDialog(this, "Select SCS# Archive to extract", "", "",
		"SCS files (*.scs)|*.scs", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
	if (fBrowser->ShowModal() == wxID_CANCEL)
	{
		return;
	}
	wstring path = fBrowser->GetPath().ToStdWstring();
	this->exSettingsGui = new SCSExtractSettingsGUI (NULL, path, exSettings);
	exSettingsGui->Show();
}

SCSExtractSettingsGUI::SCSExtractSettingsGUI(wxWindow *parent, std::wstring p, ExtractSettings* set)
	: wxDialog(parent, wxID_ANY, wxString(wxT("Extractor settings")))
{
	[...]
	wxButton *extract = new wxButton(panel, ID_ButtonExtract, wxT("Extract"));
	//Bind(wxEVT_BUTTON, &MyFrame::OnExtractButtonExtract, wxGetApp().frame, ID_ButtonExtract);
	Connect(ID_ButtonExtract, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(SCSExtractSettingsGUI::OnExtractButton));
	[...]
}
	
void SCSExtractSettingsGUI::OnExtractButton(wxCommandEvent & event)
{
	wxGetApp().frame->DoSomething();

	// TO-DO Rename to more appropiate name.
	wxGetApp().frame->DoExtract(this->path, this->settings);
}

void MyFrame::DoExtract(std::wstring path, ExtractSettings* settings)
{
	exGui = new SCSExtractGUI(NULL);
	exGui->Show();
}

Order is that:
  • MyFrame
  • FileBrowser(not needed in that case, so there's no code for it)
  • SCSExtractSettingsGUI
  • SCSExtractGUI
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problem with frame pointer

Post by doublemax »

The main problem lies here:

Code: Select all

MyFrame *frame = new MyFrame();
You're shadowing the member variable "frame" with a local variable of the same name. The member remains uninitialized.

In general:
Dialog should be created on the stack and shown with ShowModal().(This includes wxFileDialog).
If you want something non-modal, use wxFrame for it.
Use the source, Luke!
MinusPL
In need of some credit
In need of some credit
Posts: 4
Joined: Sat Jul 13, 2019 12:40 pm

Re: Problem with frame pointer

Post by MinusPL »

Thank you for your help. I didn't noticed that.
It's somewhat "beginner" mistake and I feel a little ashamed because of that, but everyone makes mistakes.
Thanks for all your time. I think that this thread can be closed now.
Keep up good work as moderator :)
Post Reply