singleton modeless dialog

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
arnoldemu
In need of some credit
In need of some credit
Posts: 7
Joined: Mon Dec 29, 2014 12:59 pm

singleton modeless dialog

Post by arnoldemu » Mon Oct 17, 2016 7:05 am

Hi,
I have a modeless dialog which is also a singleton.

The class declaration:

Code: Select all

class CAutoTypeDialog : public wxDialog
{
private:
	CAutoTypeDialog(wxWindow* parent);
	~CAutoTypeDialog();

	static CAutoTypeDialog *m_pInstance;
public:
	// creator
	static CAutoTypeDialog *CreateInstance(wxWindow *pParent)
	{
		if (m_pInstance == NULL)
		{
			m_pInstance = new CAutoTypeDialog(pParent);
			if (m_pInstance != NULL)
			{
				m_pInstance->Show();
			}

		}
		else
		{
			m_pInstance->Raise();
		}
		return m_pInstance;
	}
	static CAutoTypeDialog *GetInstance()
	{
		return m_pInstance;
	}
	virtual bool TransferDataToWindow() wxOVERRIDE;
private:
	void OnClose(wxCloseEvent &event);
	void OnOK(wxCommandEvent &event);
	void OnCancel(wxCommandEvent &event);

	// This class handles events
	DECLARE_EVENT_TABLE()
};
To create I do this:

Code: Select all

CAutoTypeDialog::CreateInstance(pParentWindow);
The implementation looks like this:

Code: Select all

#include "AutoTypeDialog.h"
#include <wx/xrc/xmlres.h>

BEGIN_EVENT_TABLE(CAutoTypeDialog, wxDialog)
EVT_CLOSE(CAutoTypeDialog::OnClose)
EVT_BUTTON(XRCID("wxID_OK"), CAutoTypeDialog::OnOK)
EVT_BUTTON(XRCID("wxID_CANCEL"), CAutoTypeDialog::OnCancel)
END_EVENT_TABLE()

CAutoTypeDialog *CAutoTypeDialog::m_pInstance = NULL;

CAutoTypeDialog::~CAutoTypeDialog()
{

	m_pInstance = NULL;
}

void CAutoTypeDialog::OnCancel(wxCommandEvent & WXUNUSED(event))
{
	Close();
}

void CAutoTypeDialog::OnOK(wxCommandEvent & WXUNUSED(event))
{
}

void CAutoTypeDialog::OnClose(wxCloseEvent & WXUNUSED(event))
{
	this->Destroy();
}

CAutoTypeDialog::CAutoTypeDialog(wxWindow *parent) : wxDialog()
{
	wxXmlResource::Get()->LoadDialog(this, parent, wxT("autotype_dialog"));
}
The dialog is created from an XRC resource. Clicking the close button calls the destroy function. Does it also call delete?

The reason I ask is I am seeing weirdness on wxMac only. wxMSW and wxGTK are fine.

My dialog has a wxTextCtrl. In the TransferDataToWindow I load text into the wxTextCtrl. I also have a drag drop handler which uses LoadFile to load the text from the dropped file into the window.

On wxMac I see two bits of text when the file is dropped. The original loaded text and the dropped text. Using Clear on the wxTextCtrl doesn't clear it.

I tried to reproduce it with the "text" sample but it was always ok when I dropped the file, so I think it's my use of a modeless dialog and how I destroy it. It seems like the dialog is not destroyed fully on wxMac or the dialog is being re-used.

So, does this design pattern work ok for wxWidgets? Should I call "delete this" in the OnClose instead?

Thanks,

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

Re: singleton modeless dialog

Post by doublemax » Mon Oct 17, 2016 7:21 am

The dialog is created from an XRC resource. Clicking the close button calls the destroy function. Does it also call delete?
No. As dialogs can be created on the heap or the stack and the dialog itself doesn't know which, it can't just call delete. (Because it would crash if it was created on the stack).
Should I call "delete this" in the OnClose instead?
Not delete, but call Destroy(). Toplevel windows use a delayed destruction to make sure that they are only destroyed when there are no more pending events.
Use the source, Luke!

Post Reply