Browse Source

Remove ReferenceCountable from StyleSheet

Michael Ragazzon 6 years ago
parent
commit
3691b70f98

+ 1 - 1
Include/RmlUi/Core/Element.h

@@ -104,7 +104,7 @@ public:
 
 	/// Returns the active style sheet for this element. This may be NULL.
 	/// @return The element's style sheet.
-	virtual StyleSheet* GetStyleSheet() const;
+	virtual const SharedPtr<StyleSheet>& GetStyleSheet() const;
 
 	/// Returns the element's definition, updating if necessary.
 	/// @return The element's definition.

+ 3 - 3
Include/RmlUi/Core/ElementDocument.h

@@ -74,9 +74,9 @@ public:
 	const String& GetSourceURL() const;
 
 	/// Sets the style sheet this document, and all of its children, uses.
-	void SetStyleSheet(StyleSheet* style_sheet);
+	void SetStyleSheet(SharedPtr<StyleSheet> style_sheet);
 	/// Returns the document's style sheet.
-	StyleSheet* GetStyleSheet() const override;
+	const SharedPtr<StyleSheet>& GetStyleSheet() const override;
 
 	/// Brings the document to the front of the document stack.
 	void PullToFront();
@@ -164,7 +164,7 @@ private:
 	String source_url;
 
 	// The document's style sheet.
-	StyleSheet* style_sheet;
+	SharedPtr<StyleSheet> style_sheet;
 
 	Context* context;
 

+ 3 - 3
Include/RmlUi/Core/Factory.h

@@ -136,15 +136,15 @@ public:
 /// Creates a style sheet from a user-generated string.
 	/// @param[in] string The contents of the style sheet.
 	/// @return A pointer to the newly created style sheet.
-	static StyleSheet* InstanceStyleSheetString(const String& string);
+	static SharedPtr<StyleSheet> InstanceStyleSheetString(const String& string);
 	/// Creates a style sheet from a file.
 	/// @param[in] file_name The location of the style sheet file.
 	/// @return A pointer to the newly created style sheet.
-	static StyleSheet* InstanceStyleSheetFile(const String& file_name);
+	static SharedPtr<StyleSheet> InstanceStyleSheetFile(const String& file_name);
 	/// Creates a style sheet from an Stream.
 	/// @param[in] stream A pointer to the stream containing the style sheet's contents.
 	/// @return A pointer to the newly created style sheet.
-	static StyleSheet* InstanceStyleSheetStream(Stream* stream);
+	static SharedPtr<StyleSheet> InstanceStyleSheetStream(Stream* stream);
 	/// Clears the style sheet cache. This will force style sheets to be reloaded.
 	static void ClearStyleSheetCache();
 	/// Clears the template cache. This will force template to be reloaded.

+ 2 - 6
Include/RmlUi/Core/StyleSheet.h

@@ -70,7 +70,7 @@ using DecoratorSpecificationMap = UnorderedMap<String, DecoratorSpecification>;
 	@author Lloyd Weehuizen
  */
 
-class RMLUICORE_API StyleSheet : public ReferenceCountable
+class RMLUICORE_API StyleSheet : public NonCopyMoveable
 {
 public:
 	typedef std::unordered_set< StyleSheetNode* > NodeList;
@@ -83,7 +83,7 @@ public:
 	bool LoadStyleSheet(Stream* stream);
 
 	/// Combines this style sheet with another one, producing a new sheet.
-	StyleSheet* CombineStyleSheet(const StyleSheet* sheet) const;
+	SharedPtr<StyleSheet> CombineStyleSheet(const StyleSheet& sheet) const;
 	/// Builds the node index for a combined style sheet, and optimizes some properties for faster retrieval.
 	/// Specifically, converts all decorator properties from strings to instanced decorator lists.
 	void BuildNodeIndexAndOptimizeProperties();
@@ -107,10 +107,6 @@ public:
 	/// caller, so another should not be added. The definition should be released by removing the reference count.
 	std::shared_ptr<ElementDefinition> GetElementDefinition(const Element* element) const;
 
-protected:
-	/// Destroys the style sheet.
-	virtual void OnReferenceDeactivate();
-
 private:
 	// Root level node, attributes from special nodes like "body" get added to this node
 	std::unique_ptr<StyleSheetNode> root;

+ 6 - 3
Source/Core/Element.cpp

@@ -333,9 +333,12 @@ String Element::GetClassNames() const
 }
 
 // Returns the active style sheet for this element. This may be NULL.
-StyleSheet* Element::GetStyleSheet() const
+const SharedPtr<StyleSheet>& Element::GetStyleSheet() const
 {
-	return style->GetStyleSheet();
+	if (ElementDocument * document = GetOwnerDocument())
+		return document->GetStyleSheet();
+	static SharedPtr<StyleSheet> null_style_sheet;
+	return null_style_sheet;
 }
 
 // Returns the element's definition, updating if necessary.
@@ -2481,7 +2484,7 @@ void Element::UpdateAnimation()
 		bool element_has_animations = (!animation_list.empty() || !animations.empty());
 		StyleSheet* stylesheet = nullptr;
 
-		if (element_has_animations && (stylesheet = GetStyleSheet()))
+		if (element_has_animations && (stylesheet = GetStyleSheet().get()))
 		{
 			// Remove existing animations
 			// Note: We are effectively restarting all animations whenever 'drty_animation' is set. Use the dirty flag with care,

+ 14 - 26
Source/Core/ElementDocument.cpp

@@ -61,8 +61,6 @@ ElementDocument::ElementDocument(const String& tag) : Element(tag)
 
 ElementDocument::~ElementDocument()
 {
-	if (style_sheet != NULL)
-		style_sheet->RemoveReference();
 }
 
 void ElementDocument::ProcessHeader(const DocumentHeader* document_header)
@@ -93,43 +91,38 @@ void ElementDocument::ProcessHeader(const DocumentHeader* document_header)
 
 	// If a style-sheet (or sheets) has been specified for this element, then we load them and set the combined sheet
 	// on the element; all of its children will inherit it by default.
-	StyleSheet* style_sheet = NULL;
+	SharedPtr<StyleSheet> new_style_sheet;
 	if (header.rcss_external.size() > 0)
-		style_sheet = StyleSheetFactory::GetStyleSheet(header.rcss_external);
+		new_style_sheet = StyleSheetFactory::GetStyleSheet(header.rcss_external);
 
 	// Combine any inline sheets.
 	if (header.rcss_inline.size() > 0)
 	{			
 		for (size_t i = 0;i < header.rcss_inline.size(); i++)
 		{			
-			StyleSheet* new_sheet = new StyleSheet();
+			std::unique_ptr<StyleSheet> inline_sheet = std::make_unique<StyleSheet>();
 			StreamMemory* stream = new StreamMemory((const byte*) header.rcss_inline[i].c_str(), header.rcss_inline[i].size());
 			stream->SetSourceURL(document_header->source);
 
-			if (new_sheet->LoadStyleSheet(stream))
+			if (inline_sheet->LoadStyleSheet(stream))
 			{
-				if (style_sheet)
+				if (new_style_sheet)
 				{
-					StyleSheet* combined_sheet = style_sheet->CombineStyleSheet(new_sheet);
-					style_sheet->RemoveReference();
-					new_sheet->RemoveReference();
-					style_sheet = combined_sheet;
+					SharedPtr<StyleSheet> combined_sheet = new_style_sheet->CombineStyleSheet(*inline_sheet);
+					new_style_sheet = combined_sheet;
 				}
 				else
-					style_sheet = new_sheet;
+					new_style_sheet = std::move(inline_sheet);
 			}
-			else
-				new_sheet->RemoveReference();
 
 			stream->RemoveReference();
 		}		
 	}
 
 	// If a style sheet is available, set it on the document and release it.
-	if (style_sheet)
+	if (new_style_sheet)
 	{
-		SetStyleSheet(style_sheet);
-		style_sheet->RemoveReference();
+		SetStyleSheet(std::move(new_style_sheet));
 	}
 
 	// Load external scripts.
@@ -177,26 +170,21 @@ const String& ElementDocument::GetSourceURL() const
 }
 
 // Sets the style sheet this document, and all of its children, uses.
-void ElementDocument::SetStyleSheet(StyleSheet* _style_sheet)
+void ElementDocument::SetStyleSheet(SharedPtr<StyleSheet> _style_sheet)
 {
 	if (style_sheet == _style_sheet)
 		return;
 
-	if (style_sheet != NULL)
-		style_sheet->RemoveReference();
-
 	style_sheet = _style_sheet;
-	if (style_sheet != NULL)
-	{
-		style_sheet->AddReference();
+	
+	if (style_sheet)
 		style_sheet->BuildNodeIndexAndOptimizeProperties();
-	}
 
 	GetStyle()->DirtyDefinition();
 }
 
 // Returns the document's style sheet.
-StyleSheet* ElementDocument::GetStyleSheet() const
+const SharedPtr<StyleSheet>& ElementDocument::GetStyleSheet() const
 {
 	return style_sheet;
 }

+ 3 - 14
Source/Core/ElementStyle.cpp

@@ -172,7 +172,7 @@ void ElementStyle::UpdateDefinition()
 
 		std::shared_ptr<ElementDefinition> new_definition;
 		
-		if (const StyleSheet * style_sheet = GetStyleSheet())
+		if (auto& style_sheet = element->GetStyleSheet())
 		{
 			new_definition = style_sheet->GetElementDefinition(element);
 		}
@@ -335,17 +335,6 @@ const PropertyMap& ElementStyle::GetLocalStyleProperties() const
 	return inline_properties.GetProperties();
 }
 
-// Returns the active style sheet for this element. This may be NULL.
-StyleSheet* ElementStyle::GetStyleSheet() const
-{
-	ElementDocument* document = element->GetOwnerDocument();
-	if (document)
-		return document->GetStyleSheet();
-
-	return nullptr;
-}
-
-
 float ElementStyle::ResolveNumberLengthPercentage(const Property * property, RelativeTarget relative_target) const
 {
 	// There is an exception on font-size properties, as 'em' units here refer to parent font size instead
@@ -854,7 +843,7 @@ DirtyPropertyList ElementStyle::ComputeValues(Style::ComputedValues& values, con
 			{
 				// Usually the decorator is converted from string after the style sheet is set on the ElementDocument. However, if the
 				// user sets a decorator on the element's style, we may still get a string here which must be parsed and instanced.
-				if(const StyleSheet* style_sheet = GetStyleSheet())
+				if(auto& style_sheet = element->GetStyleSheet())
 				{
 					String value = p->Get<String>();
 					values.decorator = style_sheet->InstanceDecoratorsFromString(value, p->source, p->source_line_number);
@@ -869,7 +858,7 @@ DirtyPropertyList ElementStyle::ComputeValues(Style::ComputedValues& values, con
 			}
 			else if (p->unit == Property::STRING)
 			{
-				if (const StyleSheet * style_sheet = GetStyleSheet())
+				if (auto & style_sheet = element->GetStyleSheet())
 				{
 					String value = p->Get<String>();
 					values.font_effect = style_sheet->InstanceFontEffectsFromString(value, p->source, p->source_line_number);

+ 0 - 3
Source/Core/ElementStyle.h

@@ -103,9 +103,6 @@ public:
 	/// Returns the local style properties, excluding any properties from local class.
 	const PropertyMap& GetLocalStyleProperties() const;
 
-	/// Returns the active style sheet for this element. This may be NULL.
-	StyleSheet* GetStyleSheet() const;
-
 	/// Resolves a property with units of length or percentage to 'px'. Percentages are resolved by scaling the base value.
 	/// @param[in] name The property to resolve the value for.
 	/// @param[in] base_value The value that is scaled by the percentage value, if it is a percentage.

+ 7 - 9
Source/Core/Factory.cpp

@@ -341,35 +341,33 @@ FontEffectInstancer* Factory::GetFontEffectInstancer(const String& name)
 
 
 // Creates a style sheet containing the passed in styles.
-StyleSheet* Factory::InstanceStyleSheetString(const String& string)
+SharedPtr<StyleSheet> Factory::InstanceStyleSheetString(const String& string)
 {
 	StreamMemory* memory_stream = new StreamMemory((const byte*) string.c_str(), string.size());
-	StyleSheet* style_sheet = InstanceStyleSheetStream(memory_stream);
+	SharedPtr<StyleSheet> style_sheet = InstanceStyleSheetStream(memory_stream);
 	memory_stream->RemoveReference();
 	return style_sheet;
 }
 
 // Creates a style sheet from a file.
-StyleSheet* Factory::InstanceStyleSheetFile(const String& file_name)
+SharedPtr<StyleSheet> Factory::InstanceStyleSheetFile(const String& file_name)
 {
 	StreamFile* file_stream = new StreamFile();
 	file_stream->Open(file_name);
-	StyleSheet* style_sheet = InstanceStyleSheetStream(file_stream);
+	SharedPtr<StyleSheet> style_sheet = InstanceStyleSheetStream(file_stream);
 	file_stream->RemoveReference();
 	return style_sheet;
 }
 
 // Creates a style sheet from an Stream.
-StyleSheet* Factory::InstanceStyleSheetStream(Stream* stream)
+SharedPtr<StyleSheet> Factory::InstanceStyleSheetStream(Stream* stream)
 {
-	StyleSheet* style_sheet = new StyleSheet();
+	SharedPtr<StyleSheet> style_sheet = std::make_shared<StyleSheet>();
 	if (style_sheet->LoadStyleSheet(stream))
 	{
 		return style_sheet;
 	}
-
-	style_sheet->RemoveReference();
-	return NULL;
+	return nullptr;
 }
 
 // Clears the style sheet cache. This will force style sheets to be reloaded.

+ 12 - 21
Source/Core/StyleSheet.cpp

@@ -66,42 +66,39 @@ bool StyleSheet::LoadStyleSheet(Stream* stream)
 }
 
 /// Combines this style sheet with another one, producing a new sheet
-StyleSheet* StyleSheet::CombineStyleSheet(const StyleSheet* other_sheet) const
+SharedPtr<StyleSheet> StyleSheet::CombineStyleSheet(const StyleSheet& other_sheet) const
 {
-	RMLUI_ASSERT(other_sheet);
-
-	StyleSheet* new_sheet = new StyleSheet();
+	SharedPtr<StyleSheet> new_sheet = std::make_shared<StyleSheet>();
 	if (!new_sheet->root->MergeHierarchy(root.get()) ||
-		!new_sheet->root->MergeHierarchy(other_sheet->root.get(), specificity_offset))
+		!new_sheet->root->MergeHierarchy(other_sheet.root.get(), specificity_offset))
 	{
-		delete new_sheet;
-		return NULL;
+		return nullptr;
 	}
 
 	// Any matching @keyframe names are overridden as per CSS rules
-	new_sheet->keyframes.reserve(keyframes.size() + other_sheet->keyframes.size());
+	new_sheet->keyframes.reserve(keyframes.size() + other_sheet.keyframes.size());
 	new_sheet->keyframes = keyframes;
-	for (auto& other_keyframes : other_sheet->keyframes)
+	for (auto& other_keyframes : other_sheet.keyframes)
 	{
 		new_sheet->keyframes[other_keyframes.first] = other_keyframes.second;
 	}
 
 	// Copy over the decorators, and replace any matching decorator names from other_sheet
-	new_sheet->decorator_map.reserve(decorator_map.size() + other_sheet->decorator_map.size());
+	new_sheet->decorator_map.reserve(decorator_map.size() + other_sheet.decorator_map.size());
 	new_sheet->decorator_map = decorator_map;
-	for (auto& other_decorator: other_sheet->decorator_map)
+	for (auto& other_decorator: other_sheet.decorator_map)
 	{
 		new_sheet->decorator_map[other_decorator.first] = other_decorator.second;
 	}
 
 	new_sheet->spritesheet_list.Reserve(
-		spritesheet_list.NumSpriteSheets() + other_sheet->spritesheet_list.NumSpriteSheets(),
-		spritesheet_list.NumSprites() + other_sheet->spritesheet_list.NumSprites()
+		spritesheet_list.NumSpriteSheets() + other_sheet.spritesheet_list.NumSpriteSheets(),
+		spritesheet_list.NumSprites() + other_sheet.spritesheet_list.NumSprites()
 	);
-	new_sheet->spritesheet_list = other_sheet->spritesheet_list;
+	new_sheet->spritesheet_list = other_sheet.spritesheet_list;
 	new_sheet->spritesheet_list.Merge(spritesheet_list);
 
-	new_sheet->specificity_offset = specificity_offset + other_sheet->specificity_offset;
+	new_sheet->specificity_offset = specificity_offset + other_sheet.specificity_offset;
 	return new_sheet;
 }
 
@@ -364,11 +361,5 @@ std::shared_ptr<ElementDefinition> StyleSheet::GetElementDefinition(const Elemen
 	return new_definition;
 }
 
-// Destroys the style sheet.
-void StyleSheet::OnReferenceDeactivate()
-{
-	delete this;
-}
-
 }
 }

+ 14 - 28
Source/Core/StyleSheetFactory.cpp

@@ -91,29 +91,27 @@ void StyleSheetFactory::Shutdown()
 	}
 }
 
-StyleSheet* StyleSheetFactory::GetStyleSheet(const String& sheet_name)
+SharedPtr<StyleSheet> StyleSheetFactory::GetStyleSheet(const String& sheet_name)
 {
 	// Look up the sheet definition in the cache
 	StyleSheets::iterator itr = instance->stylesheets.find(sheet_name);
 	if (itr != instance->stylesheets.end())
 	{
-		(*itr).second->AddReference();
 		return (*itr).second;
 	}
 
 	// Don't currently have the sheet, attempt to load it
-	StyleSheet* sheet = instance->LoadStyleSheet(sheet_name);
-	if (sheet == NULL)
-		return NULL;
+	SharedPtr<StyleSheet> sheet = instance->LoadStyleSheet(sheet_name);
+	if (!sheet)
+		return nullptr;
 
 	// Add it to the cache, and add a reference count so the cache will keep hold of it.
 	instance->stylesheets[sheet_name] = sheet;
-	sheet->AddReference();
 
 	return sheet;
 }
 
-StyleSheet* StyleSheetFactory::GetStyleSheet(const StringList& sheets)
+SharedPtr<StyleSheet> StyleSheetFactory::GetStyleSheet(const StringList& sheets)
 {
 	// Generate a unique key for these sheets
 	String combined_key;
@@ -127,23 +125,19 @@ StyleSheet* StyleSheetFactory::GetStyleSheet(const StringList& sheets)
 	StyleSheets::iterator itr = instance->stylesheet_cache.find(combined_key);
 	if (itr != instance->stylesheet_cache.end())
 	{
-		(*itr).second->AddReference();
 		return (*itr).second;
 	}
 
 	// Load and combine the sheets.
-	StyleSheet* sheet = NULL;
+	SharedPtr<StyleSheet> sheet;
 	for (size_t i = 0; i < sheets.size(); i++)
 	{
-		StyleSheet* sub_sheet = GetStyleSheet(sheets[i]);
+		SharedPtr<StyleSheet> sub_sheet = GetStyleSheet(sheets[i]);
 		if (sub_sheet)
 		{
 			if (sheet)
 			{
-				StyleSheet* new_sheet = sheet->CombineStyleSheet(sub_sheet);
-				sheet->RemoveReference();
-				sub_sheet->RemoveReference();
-
+				SharedPtr<StyleSheet> new_sheet = sheet->CombineStyleSheet(*sub_sheet);
 				sheet = new_sheet;
 			}
 			else
@@ -153,24 +147,17 @@ StyleSheet* StyleSheetFactory::GetStyleSheet(const StringList& sheets)
 			Log::Message(Log::LT_ERROR, "Failed to load style sheet %s.", sheets[i].c_str());
 	}
 
-	if (sheet == NULL)
-		return NULL;
+	if (!sheet)
+		return nullptr;
 
 	// Add to cache, and a reference to the sheet to hold it in the cache.
 	instance->stylesheet_cache[combined_key] = sheet;
-	sheet->AddReference();
 	return sheet;
 }
 
 // Clear the style sheet cache.
 void StyleSheetFactory::ClearStyleSheetCache()
 {
-	for (StyleSheets::iterator i = instance->stylesheets.begin(); i != instance->stylesheets.end(); ++i)
-		(*i).second->RemoveReference();
-
-	for (StyleSheets::iterator i = instance->stylesheet_cache.begin(); i != instance->stylesheet_cache.end(); ++i)
-		(*i).second->RemoveReference();
-
 	instance->stylesheets.clear();
 	instance->stylesheet_cache.clear();
 }
@@ -185,20 +172,19 @@ StyleSheetNodeSelector* StyleSheetFactory::GetSelector(const String& name)
 	return (*i).second;
 }
 
-StyleSheet* StyleSheetFactory::LoadStyleSheet(const String& sheet)
+SharedPtr<StyleSheet> StyleSheetFactory::LoadStyleSheet(const String& sheet)
 {
-	StyleSheet* new_style_sheet = NULL;
+	SharedPtr<StyleSheet> new_style_sheet;
 
 	// Open stream, construct new sheet and pass the stream into the sheet
 	// TODO: Make this support ASYNC
 	StreamFile* stream = new StreamFile();
 	if (stream->Open(sheet))
 	{
-		new_style_sheet = new StyleSheet();
+		new_style_sheet = std::make_shared<StyleSheet>();
 		if (!new_style_sheet->LoadStyleSheet(stream))
 		{
-			new_style_sheet->RemoveReference();
-			new_style_sheet = NULL;
+			new_style_sheet = nullptr;
 		}
 	}
 

+ 4 - 4
Source/Core/StyleSheetFactory.h

@@ -53,12 +53,12 @@ public:
 
 	/// Gets the named sheet, retrieving it from the cache if its already been loaded
 	/// @param sheet name of sheet to load
-	static StyleSheet* GetStyleSheet(const String& sheet);
+	static SharedPtr<StyleSheet> GetStyleSheet(const String& sheet);
 
 	/// Builds and returns a stylesheet based on the list of input sheets
 	/// Generated sheets will be cached for later use
 	/// @param sheets List of sheets to combine into one	
-	static StyleSheet* GetStyleSheet(const StringList& sheets);
+	static SharedPtr<StyleSheet> GetStyleSheet(const StringList& sheets);
 
 	/// Clear the style sheet cache.
 	static void ClearStyleSheetCache();
@@ -73,10 +73,10 @@ private:
 	~StyleSheetFactory();
 
 	// Loads an individual style sheet
-	StyleSheet* LoadStyleSheet(const String& sheet);
+	SharedPtr<StyleSheet> LoadStyleSheet(const String& sheet);
 
 	// Individual loaded stylesheets
-	typedef UnorderedMap<String, StyleSheet*> StyleSheets;
+	typedef UnorderedMap<String, SharedPtr<StyleSheet>> StyleSheets;
 	StyleSheets stylesheets;
 
 	// Cache of combined style sheets

+ 3 - 4
Source/Debugger/ElementInfo.cpp

@@ -61,12 +61,11 @@ bool ElementInfo::Initialise()
 	AddEventListener(Core::EventId::Click, this);
 	AddEventListener(Core::EventId::Mouseover, this);
 
-	Core::StyleSheet* style_sheet = Core::Factory::InstanceStyleSheetString(Core::String(common_rcss) + Core::String(info_rcss));
-	if (style_sheet == NULL)
+	Core::SharedPtr<Core::StyleSheet> style_sheet = Core::Factory::InstanceStyleSheetString(Core::String(common_rcss) + Core::String(info_rcss));
+	if (!style_sheet)
 		return false;
 
-	SetStyleSheet(style_sheet);
-	style_sheet->RemoveReference();
+	SetStyleSheet(std::move(style_sheet));
 
 	return true;
 }

+ 7 - 10
Source/Debugger/ElementLog.cpp

@@ -92,18 +92,17 @@ bool ElementLog::Initialise()
 		message_content->AddEventListener(Core::EventId::Resize, this);
 	}
 
-	Core::StyleSheet* style_sheet = Core::Factory::InstanceStyleSheetString(Core::String(common_rcss) + Core::String(log_rcss));
-	if (style_sheet == NULL)
+	Core::SharedPtr<Core::StyleSheet> style_sheet = Core::Factory::InstanceStyleSheetString(Core::String(common_rcss) + Core::String(log_rcss));
+	if (!style_sheet)
 		return false;
 
-	SetStyleSheet(style_sheet);
-	style_sheet->RemoveReference();
+	SetStyleSheet(std::move(style_sheet));
 
 	AddEventListener(Core::EventId::Click, this);
 
 	// Create the log beacon.
 	beacon = GetContext()->CreateDocument();
-	if (beacon == NULL)
+	if (!beacon)
 		return false;
 
 	beacon->SetId("rmlui-debug-log-beacon");
@@ -111,20 +110,18 @@ bool ElementLog::Initialise()
 	beacon->SetInnerRML(beacon_rml);
 
 	Core::Element* button = beacon->GetFirstChild();
-	if (button != NULL)
+	if (button)
 		beacon->GetFirstChild()->AddEventListener(Core::EventId::Click, this);
 
 	style_sheet = Core::Factory::InstanceStyleSheetString(Core::String(common_rcss) + Core::String(beacon_rcss));
-	if (style_sheet == NULL)
+	if (!style_sheet)
 	{
 		GetContext()->UnloadDocument(beacon);
-		beacon = NULL;
-
+		beacon = nullptr;
 		return false;
 	}
 
 	beacon->SetStyleSheet(style_sheet);
-	style_sheet->RemoveReference();
 
 	return true;
 }

+ 5 - 7
Source/Debugger/Plugin.cpp

@@ -281,24 +281,22 @@ bool Plugin::LoadFont()
 bool Plugin::LoadMenuElement()
 {
 	menu_element = host_context->CreateDocument();
-	if (menu_element == NULL)
+	if (!menu_element)
 		return false;
 
 	menu_element->SetId("rmlui-debug-menu");
 	menu_element->SetProperty(Core::PropertyId::Visibility, Core::Property(Core::Style::Visibility::Hidden));
 	menu_element->SetInnerRML(menu_rml);
 
-	Core::StyleSheet* style_sheet = Core::Factory::InstanceStyleSheetString(menu_rcss);
-	if (style_sheet == NULL)
+	Core::SharedPtr<Core::StyleSheet> style_sheet = Core::Factory::InstanceStyleSheetString(menu_rcss);
+	if (!style_sheet)
 	{
 		host_context->UnloadDocument(menu_element);
-		menu_element = NULL;
-
+		menu_element = nullptr;
 		return false;
 	}
 
-	menu_element->SetStyleSheet(style_sheet);
-	style_sheet->RemoveReference();
+	menu_element->SetStyleSheet(std::move(style_sheet));
 
 	// Set the version info in the menu.
 	menu_element->GetElementById("version-number")->SetInnerRML("v" + Rml::Core::GetVersion());