Sfoglia il codice sorgente

Make initialization of Event cleaner, warning when it is constructed outside the factory (instead of undefined behavior)

Michael Ragazzon 6 anni fa
parent
commit
79cbad7abd
2 ha cambiato i file con 16 aggiunte e 31 eliminazioni
  1. 11 11
      Include/RmlUi/Core/Event.h
  2. 5 20
      Source/Core/Event.cpp

+ 11 - 11
Include/RmlUi/Core/Event.h

@@ -120,30 +120,30 @@ public:
 protected:
 	Dictionary parameters;
 
-	Element* target_element;
-	Element* current_element;
+	Element* target_element = nullptr;
+	Element* current_element = nullptr;
 
 private:
 	/// Project the mouse coordinates to the current element to enable
 	/// interacting with transformed elements.
 	void ProjectMouse(Element* element);
 
-	/// Release this event.
+	/// Release this event through its instancer.
 	void Release() override;
 
 	String type;
-	EventId id;
-	bool interruptible;
+	EventId id = EventId::Invalid;
+	bool interruptible = false;
 	
-	bool interrupted;
-	bool interrupted_immediate;
+	bool interrupted = false;
+	bool interrupted_immediate = false;
 
-	bool has_mouse_position;
-	Vector2f mouse_screen_position;
+	bool has_mouse_position = false;
+	Vector2f mouse_screen_position = Vector2f(0, 0);
 
-	EventPhase phase;
+	EventPhase phase = EventPhase::None;
 
-	EventInstancer* instancer;
+	EventInstancer* instancer = nullptr;
 
 	friend class Factory;
 };

+ 5 - 20
Source/Core/Event.cpp

@@ -33,30 +33,13 @@
 namespace Rml {
 namespace Core {
 
-Event::Event() : id(EventId::Invalid)
+Event::Event()
 {
-	id = EventId::Invalid;
-	phase = EventPhase::None;
-	interruptible = false;
-	interrupted = false;
-	interrupted_immediate = false;
-	current_element = nullptr;
-	target_element = nullptr;
-	has_mouse_position = false;
-	mouse_screen_position = Vector2f(0, 0);
 }
 
 Event::Event(Element* _target_element, EventId id, const String& type, const Dictionary& _parameters, bool interruptible)
 	: parameters(_parameters), target_element(_target_element), type(type), id(id), interruptible(interruptible)
 {
-	phase = EventPhase::None;
-	interrupted = false;
-	interrupted_immediate = false;
-	current_element = nullptr;
-
-	has_mouse_position = false;
-	mouse_screen_position = Vector2f(0, 0);
-
 	const Variant* mouse_x = GetIf(parameters, "mouse_x");
 	const Variant* mouse_y = GetIf(parameters, "mouse_y");
 	if (mouse_x && mouse_y)
@@ -122,7 +105,6 @@ bool Event::IsPropagating() const
 
 void Event::StopPropagation()
 {
-	// Set interrupted to true if we can be interrupted
 	if (interruptible)
 	{
 		interrupted = true;
@@ -160,7 +142,10 @@ const Vector2f& Event::GetUnprojectedMouseScreenPos() const
 
 void Event::Release()
 {
-	instancer->ReleaseEvent(this);
+	if (instancer)
+		instancer->ReleaseEvent(this);
+	else
+		Log::Message(Log::LT_WARNING, "Leak detected: Event %s not instanced via RmlUi Factory. Unable to release.", type.c_str());
 }
 
 EventId Event::GetId() const