Browse Source

Fixed potential crash if applying console style caused new messages to be logged.
As UIElement::GetAppliedStyle() returns an empty string for the 'auto' style, also allow to use SetStyle() with empty string to apply the auto style.

Lasse Öörni 12 years ago
parent
commit
f950220350
2 changed files with 17 additions and 8 deletions
  1. 11 5
      Source/Engine/Engine/Console.cpp
  2. 6 3
      Source/Engine/UI/UIElement.cpp

+ 11 - 5
Source/Engine/Engine/Console.cpp

@@ -96,12 +96,15 @@ void Console::SetDefaultStyle(XMLFile* style)
     background_->SetDefaultStyle(style);
     background_->SetStyle("ConsoleBackground");
 
-    const Vector<SharedPtr<UIElement> >& children = rowContainer_->GetChildren();
+    // Setting the style may cause us to log messages, which will delete and remove the child objects.
+    // For safety, iterate through our own copy of the child element vector, which will keep the current
+    // text elements alive
+    Vector<SharedPtr<UIElement> > children = rowContainer_->GetChildren();
     for (unsigned i = 0; i < children.Size(); ++i)
-        children[i]->SetStyle("ConsoleText");
-
+        children[i]->SetStyle(children[i]->GetAppliedStyle());
+    
     lineEdit_->SetStyle("ConsoleLineEdit");
-
+    
     UpdateElements();
 }
 
@@ -144,7 +147,10 @@ void Console::SetNumRows(unsigned rows)
     else
     {
         for (int i = 0; i > delta; --i)
-            rowContainer_->CreateChild<Text>();
+        {
+            Text* row = rowContainer_->CreateChild<Text>();
+            row->SetStyle("ConsoleText");
+        }
     }
 
     rowContainer_->EnableLayoutUpdate();

+ 6 - 3
Source/Engine/UI/UIElement.cpp

@@ -883,7 +883,10 @@ void UIElement::SetDragDropMode(unsigned mode)
 
 bool UIElement::SetStyle(const String& styleName, XMLFile* file)
 {
-    appliedStyle_ = styleName;
+    // If empty style was requested, replace with type name
+    String actualStyleName = !styleName.Empty() ? styleName : GetTypeName();
+    
+    appliedStyle_ = actualStyleName;
     if (styleName == "none")
         return true;
 
@@ -899,7 +902,7 @@ bool UIElement::SetStyle(const String& styleName, XMLFile* file)
         defaultStyle_ = file;
     }
 
-    styleXPathQuery_.SetVariable("typeName", styleName);
+    styleXPathQuery_.SetVariable("typeName", actualStyleName);
     XMLElement styleElem = file->GetRoot().SelectSinglePrepared(styleXPathQuery_);
     return styleElem && SetStyle(styleElem);
 }
@@ -914,7 +917,7 @@ bool UIElement::SetStyle(const XMLElement& element)
 
 bool UIElement::SetStyleAuto(XMLFile* file)
 {
-    return SetStyle(GetTypeName(), file);
+    return SetStyle(String::EMPTY, file);
 }
 
 void UIElement::SetDefaultStyle(XMLFile* style)