diff engine/core/view/renderers/instancerenderer.cpp @ 564:7d88eddd2ec7

Modified the addColored and addOutlined methods in the InstanceRenderer class to fix a memory leak. The new code attempts to insert the information in the proper map and if it already exists it will explicitly update the reference to the existing information. The new code no longer relies on the bracket operator of the std::map which was implicitly calling the equals operator of the ColoringInfo and OutlineInfo and causing the memory leak. fixes[ticket:472]
author vtchill@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 26 Jun 2010 00:42:14 +0000
parents 5987f78a2364
children edc9efe969c2
line wrap: on
line diff
--- a/engine/core/view/renderers/instancerenderer.cpp	Thu Jun 24 21:27:11 2010 +0000
+++ b/engine/core/view/renderers/instancerenderer.cpp	Sat Jun 26 00:42:14 2010 +0000
@@ -259,22 +259,51 @@
 	}
 
 	void InstanceRenderer::addOutlined(Instance* instance, int r, int g, int b, int width) {
-		OutlineInfo info;
-		info.r = r;
-		info.g = g;
-		info.b = b;
-		info.width = width;
+		OutlineInfo newinfo;
+		newinfo.r = r;
+		newinfo.g = g;
+		newinfo.b = b;
+		newinfo.width = width;
 
-		m_instance_outlines[instance] = info;
+		// attempts to insert the element into the outline map
+		// will return false in the second value of the pair if the instance already exists 
+		// in the map and the first value of the pair will then be an iterator to the 
+		// existing data for the instance
+		std::pair<InstanceToOutlines_t::iterator, bool> insertiter = m_instance_outlines.insert(std::make_pair(instance, newinfo));
+
+		if (insertiter.second == false)
+		{
+			// the insertion did not happen because the instance 
+			// already exists in the map so lets just update its outline info
+			OutlineInfo& info = insertiter.first->second;
+			info.r = r;
+			info.b = b;
+			info.g = g;
+			info.width = width;
+		}
 	}
 
 	void InstanceRenderer::addColored(Instance* instance, int r, int g, int b) {
-		ColoringInfo info;
-		info.r = r;
-		info.g = g;
-		info.b = b;
+		ColoringInfo newinfo;
+		newinfo.r = r;
+		newinfo.g = g;
+		newinfo.b = b;
+		
+		// attempts to insert the element into the coloring map
+		// will return false in the second value of the pair if the instance already exists 
+		// in the map and the first value of the pair will then be an iterator to the 
+		// existing data for the instance
+		std::pair<InstanceToColoring_t::iterator, bool> insertiter = m_instance_colorings.insert(std::make_pair(instance, newinfo));
 
-		m_instance_colorings[instance] = info;
+		if (insertiter.second == false)
+		{
+			// the insertion did not happen because the instance 
+			// already exists in the map so lets just update its coloring info
+			ColoringInfo& info = insertiter.first->second;
+			info.r = r;
+			info.b = b;
+			info.g = g;
+		}
 	}
 
 	void InstanceRenderer::removeOutlined(Instance* instance) {