#### Archived

This topic is now archived and is closed to further replies.

# A hard 'un. - Home made GUIs.

This topic is 6392 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Thanks to the excellent developing a gui in directx series of articles, I''ve developed an (almost) functional GUI myself. However one thing which has confused me is the question of allocating and deallocating memory at runtime for the windows. For example, my msgbox code looks like this:
///////////////////////////////
// MESSAGEBOX
class
CMFBDeleteParent : public CMetaformButton {
virtual int fm_lclick(int x_in, int y_in) {
if (m_pParent)
::delete m_pParent;
return (1);
}
};

void
MsgBox(CMetaform *parent, char *text, char *caption, int type) {
int height = 0;

if (!parent)
return;

CMetaformPanel *MFP = ::new CMetaformPanel;
CMetaformStaticGDI *MFSGDI = ::new CMetaformStaticGDI;
CMFBDeleteParent *MFBDeleteParentOk = ::new CMFBDeleteParent;

if (!MFP || !MFSGDI || !MFBDeleteParentOk) {
g_Console.Message("Insufficient Memory!");
return;
}

height = MFSGDI->HeightWithWidth(380);
MFSGDI->SetSize(380, height);
MFBDeleteParentOk->Initialise();
MFBDeleteParentOk->SetSize(60, 20);
MFBDeleteParentOk->SetPos(180, height + 20 + 10);
MFBDeleteParentOk->SetCaption("OK");
MFP->Initialise(caption, (parent->GetWidth()/2)-200, (parent->GetHeight()/2)-((height + 50)/2), 400, height + 60);
MFSGDI->SetPos(10, MFP->GetCharHeight() + 5);
}
// END MESSAGEBOX
///////////////////////////////

And the destructor for all the windows is like this:

CMetaform::~CMetaform(void) {
CMetaform *child;
char buf[128];

vector::iterator first = m_SubForms.begin();
vector::iterator last  = m_SubForms.end();

if (m_pParent)
m_pParent->RemoveMetaform(this);

while (first != last) {
child = *(first++);
if (child) {
sprintf(buf, "This is %s, deleting %s", caption, child->GetCaption());
g_Console.Message(buf);
g_ErrorLog.Log(buf);
::delete &child;
}
}

if (lpddsMetaform)
lpddsMetaform->Release();

sprintf(buf, "This is %s, deleting self.", caption);
g_Console.Message(buf);
g_ErrorLog.Log(buf);
}

This more or less works fine. However a msgbox is deleted by clicking on its CMFBDeleteParent button, the next time I try to create a msgbox the program exits. I suspect the problem lies in the way the windows are being created and deleted. There wasn''t any sample code in the tutorial of this, so I have no idea how correct this is. It seems to make sense, however. In the GUIs you people implement, how are the windows dynamically created and destroyed, for example with a msgbox? m_SubForms is an STL vector of CMetaform pointers. I''ve heard somewhere using pointers with STL is a quick way to arrange a meeting with El Diablo himself, but everything else seems to work fine. This is, however, driving me truly insane. What am I doing wrong here? Do you need further code for a diagnosis :?) r. "The mere thought hadn''t even begun to speculate about the slightest possibility of traversing the eternal wasteland that is my mind..."

##### Share on other sites
The way I have my GUI set up, all windows are created when the GUI is loaded from a file, and all are destroyed before loading a new GUI. But the GUI keeps a list of pointers to any windows that are visible or active, and only draws/processes them. The windows in that array are drawn first to last, so z-ordering is done by adjusting the position in that array of each pointer. It''s nice and fast, although it''s a little more of a memory hog than your implementation.

So to display a message box, I''d say pMessageBox->Show( ) and later pMessageBox->Hide( ).

~CGameProgrammer( );

##### Share on other sites
Thanks for thinking about it - but that wasn''t the question I asked. My problem is creating windows at any time during runtime, say halfway during the my program running, then destroying them, and then creating more. Am I doing that correctly as my code indicates?

"The mere thought hadn''t even begun to speculate about the slightest possibility of traversing the eternal wasteland that is my mind..."

##### Share on other sites
You can have multiple children but only one parent. If you have two children destroyed, they both try to delete the parent.

Instead, have each parent destroy its children.

##### Share on other sites
Null_pointer, that''s what I *am* doing. The button on the msgbox is of the clas CMFBDeleteParent, which when clicked calls for its parent to be deleted (and I therefore I assume has it''s destructor called) which then as the destructor shows, iterates recursively through it''s children, calling for them to be deleted, which should cause them to call for their children to be deleted. This is why I am confused. Why should this cause problems?

"The mere thought hadn''t even begun to speculate about the slightest possibility of traversing the eternal wasteland that is my mind..."

##### Share on other sites
Some strange code there... However, I should have looked at it more carefully.

Anyway, if you are using CMetaform* as template parameter T to vector, then the dereferencing operator for the iterator will return CMetaform*. Therefore the variable "child" is a pointer. Why are you calling delete on the address of the pointer? It should be "delete child;"

(Probably part of some old code - I hate coding GUIs, don''t you? )

##### Share on other sites
Ah, all too true .
Thank you.
r.

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 18
• ### Forum Statistics

• Total Topics
632160
• Total Posts
3004497

×