I recently found out that Materials are not garbage collected in the regular GC pass.
This causes our project to leak materials at a prodigious rate.
So I added some code to pool and reuse materials:
//! add rendering components to a gameobject
public override void AddComponents(SWFCRenderable obj)
{
base.AddComponents(obj);
// create mesh filter and mesh renderer
MeshRenderer render = obj.MeshRender;
render.enabled = true;
obj.Filter.mesh = m_mesh;
// duplicate each of the materials so we can use renderQueue
Material[] newMats = Dictionary.AllocateMaterials(m_materials.Length);
for (int i = 0; i < m_materials.Length; i++)
{
newMats<em>.CopyPropertiesFromMaterial(m_materials*);*</em>
newMats.shader = m_materials*.shader;
_}
render.materials = newMats;
}
//! remove rendering components from a gameobject*
public override void RemoveComponents(SWFCRenderable obj)
{
Dictionary.FreeMaterials(obj.MeshRender.materials);
base.RemoveComponents(obj);
obj.MeshRender = null;
obj.Filter = null;
}
The problem is that I’m still leaking materials.
My best guess is that setting render.materials does not properly flag the renderer as having local materials… So the next time that renderer.materials is read, it clones the materials in the array over again.
Does anybody have any insight into what’s going on, or how I can get these materials to stop leaking?_