Okay, I have a function which reads a xml file and creates controls using new and stores them in public member variables of a class called Window
:
std::map<const char*, Button*> Buttons;
std::map<const char*, TextBox*> TextBoxes;
std::map<const char*, CheckBox*> CheckBoxes;
The Button, TextBox and CheckBox classes are homemade wrappers of CreateWindowEx.
Here is the function that populates the maps:
void Window::LoadFromXml(const char* fileName)
{
XMLNode root = XMLNode::openFileHelper(fileName, "Window");
for(int i = 0; i < root.nChildNode("Button"); i++)
{
Buttons.insert(std::pair<const char*, Button*>(root.getChildNode("Button", i).getAttribute("Name"), new Button));
Buttons[root.getChildNode("Button", i).getAttribute("Name")]->Init(_handle);
}
for(int i = 0; i < root.nChildNode("CheckBox"); i++)
{
CheckBoxes.insert(std::pair<const char*, CheckBox*>(root.getChildNode("Button", i).getAttribute("CheckBox"), new CheckBox));
CheckBoxes[root.getChildNode("CheckBox", i).getAttribute("Name")]->Init(_handle);
}
for(int i = 0; i < root.nChildNode("TextBox"); i++)
{
TextBoxes.insert(std::pair<const char*, TextBox*>(root.getChildNode("TextBox", i).getAttribute("Name"), new TextBox));
TextBoxes[root.getChildNode("TextBox", i).getAttribute("Name")]->Init(_handle);
}
}
Here is the xml file:
<Window>
<TextBox Name="Email" />
<TextBox Name="Password" />
<CheckBox Name="SaveEmail" />
<CheckBox Name="SavePassword" />
<Button Name="Login" />
</Window>
The problem is, if I try to access, for example, TextBoxes["Email"]->Width(10);
, the program compiles fine, but then crashes when I start it.
I'm calling it from a derived class:
class LoginWindow : public Window
{
public:
bool OnInit(void) // This function is called by Window after CreateWindowEx and a hwnd == NULL check
{
this->LoadFromXml("xml\\LoginWindow.xml"); // the file path is right
this->TextBoxes["Email"]->Width(10); // Crash, if I remove this it works and all the controls are there
}
}