Przeglądaj źródła

Fix two memory leaks.

Michael Ragazzon 5 lat temu
rodzic
commit
ba0a5190dc

+ 5 - 7
Include/RmlUi/Core/ElementScroll.h

@@ -30,6 +30,7 @@
 #define RMLUI_CORE_ELEMENTSCROLL_H
 
 #include "Header.h"
+#include "Types.h"
 
 namespace Rml {
 
@@ -81,19 +82,16 @@ public:
 	/// Formats the enabled scrollbars based on the current size of the host element.
 	void FormatScrollbars();
 
-	/// Clears the scrollbars, resetting it to initial conditions.
-	void ClearScrollbars();
-
 private:
 	struct Scrollbar
 	{
 		Scrollbar();
 		~Scrollbar();
 
-		Element* element;
-		WidgetScroll* widget;
-		bool enabled;
-		float size;
+		Element* element = nullptr;
+		UniquePtr<WidgetScroll> widget;
+		bool enabled = false;
+		float size = 0;
 	};
 
 	// Creates one of the scroll component's scrollbar.

+ 2 - 0
Samples/shell/src/Shell.cpp

@@ -118,6 +118,8 @@ static Rml::StringList ListFilesOrDirectories(ListType type, const Rml::String&
 			result.push_back(file_list[i]->d_name);
 		}
 	}
+	for (int i = 0; i < file_count; i++)
+		free(file_list[i]);
 	free(file_list);
 #endif
 

+ 0 - 3
Source/Core/Element.cpp

@@ -160,9 +160,6 @@ Element::~Element()
 
 	PluginRegistry::NotifyElementDestroy(this);
 
-	// Remove scrollbar elements before we delete the children!
-	meta->scroll.ClearScrollbars();
-
 	// A simplified version of RemoveChild() for destruction.
 	for (ElementPtr& child : children)
 	{

+ 4 - 37
Source/Core/ElementScroll.cpp

@@ -43,26 +43,7 @@ ElementScroll::ElementScroll(Element* _element)
 }
 
 ElementScroll::~ElementScroll()
-{
-	ClearScrollbars();
-}
-
-void ElementScroll::ClearScrollbars()
-{
-	for (int i = 0; i < 2; i++)
-	{
-		if (scrollbars[i].element != nullptr)
-		{
-			scrollbars[i] = Scrollbar();
-		}
-	}
-
-	if (corner != nullptr)
-	{
-		corner->GetParentNode()->RemoveChild(element);
-		corner = nullptr;
-	}
-}
+{}
 
 // Updates the increment / decrement arrows.
 void ElementScroll::Update()
@@ -238,7 +219,7 @@ bool ElementScroll::CreateScrollbar(Orientation orientation)
 	scrollbars[orientation].element = scrollbar_element.get();
 	scrollbars[orientation].element->SetProperty(PropertyId::Clip, Property(1, Property::NUMBER));
 
-	scrollbars[orientation].widget = new WidgetScroll(scrollbars[orientation].element);
+	scrollbars[orientation].widget = MakeUnique<WidgetScroll>(scrollbars[orientation].element);
 	scrollbars[orientation].widget->Initialise(orientation == VERTICAL ? WidgetScroll::VERTICAL : WidgetScroll::HORIZONTAL);
 
 	Element* child = element->AppendChild(std::move(scrollbar_element), false);
@@ -263,23 +244,9 @@ bool ElementScroll::CreateCorner()
 }
 
 ElementScroll::Scrollbar::Scrollbar()
-{
-	element = nullptr;
-	widget = nullptr;
-	enabled = false;
-	size = 0;
-}
+{}
 
 ElementScroll::Scrollbar::~Scrollbar()
-{
-	if (widget != nullptr)
-		delete widget;
-
-	if (element != nullptr)
-	{
-		if (element->GetParentNode() != nullptr)
-			element->GetParentNode()->RemoveChild(element);
-	}
-}
+{}
 
 } // namespace Rml