Can't Destroy texture [Solved ]

After few days of investigation, finally I found one of the reason that may cause the downloaded texture can’t be destroyed.

NEVER assign the WWW.texture more than once…

even like this…

if ([url]www.texture.height[/url] > 16) { /*...*/ }

Is it interesting ? Should I submit that to bug report ??

as see on the following test code, passing the www.texture to a local variable will save the life :stuck_out_tongue:

var texture:Array = new Array();
var frame:int = 0;
var busy:int = 0;
var calls:int = 0;

var g:GameObject;

function Start ()	
{ 
   g = new GameObject.CreatePrimitive(PrimitiveType.Plane); 
}

function Update()	
{ 
   if (busy < 8) { loader(); } 
}

function loader()
{
	busy++;

	var www = new WWW("http://homepage.mac.com/antonioh/dev/loading_jpg/"+frame+".jpg");
	
	yield www;
	
	var _temp:Texture2D = [url]www.texture;[/url]
	texture.Push(_temp);

	var r:int = parseInt(Random.Range(0, texture.length-1));
	if(texture.length > 256)
	{ 
		Destroy(texture[r]); 
		texture.RemoveAt(r);
	}
	
	//if([url]www.texture.height[/url] > 16) { /* BEWARE! memory overflow ahead :p*/ }	
	if(_temp.height > 16) { /* the texture's memory return to OS  :) */ }
	
	g.renderer.material.mainTexture = _temp;

	www.Dispose();
	www = null;
	
	frame = (frame+1)%1024;
	busy--;
	calls++; 
}

function OnGUI()
{
	str=GUI.TextField(Rect(12,12,200,24),"length: "+texture.length+" calls: "+calls);
}

Hope the above information is helpful for someone like to download thousands of texture very often :slight_smile:

Have a nice weekend to all,
Antonio Hui

Hi I just wanted to say thanks … your post led me to tracking down a memory issue using www.LoadImageIntoTexture. If the texture is referenced before assigning it to the material, the temporary memory used for the download and assignment does not get freeded up. The memory used by the texture itself will get freeded in both use cases.

	// Define atlasData
	Texture2D atlasData;
	
	
	IEnumerator download (string url)
	{

		WWW www;

		www = new WWW (url);
		yield return www;
		
		Debug.Log ("www error?= " + www.error);

		// This one does not leak
		// atlasData = new Texture2D(256, 256, TextureFormat.RGB24, false); 
		
		// This one leaks
		atlasData = new Texture2D(www.texture.width, www.texture.height , TextureFormat.RGB24, false); 
		
		www.LoadImageIntoTexture(atlasData); 
		go.renderer.sharedMaterials[1].mainTexture = atlasData;
		www.Dispose();
		www = null;
		
		// Note:  atlasData is destroyed elsewhere before returning to this function

	}