Memory leak with WWW on iOS

Hello,

I have an image sensor hooked to a local server that takes roughly 10 images per second. With my Unity iOS app, I’m trying to display that camera feed.

It works well when I run the Unity app on my computer but on the iOS app, although the image is displayed and updated, the memory goes high very fast until it stops my app.

When I analyse the app memory allocation with Instruments “GFX Texture Level” and “Malloc 228.0 KB” are getting high very fast.

I saw that other users had similar issue with the WWW class on iOS not releasing correctly objects… I wanted to know if that still the case with the current Unity version (5.3.2)? If yes how could I fix this?

Here is my code:

void Update () {
	StartCoroutine (DownloadImage ());
}

IEnumerator DownloadImage(){

	WWW www = new WWW("http://192.168.2.188/tele/image.jpg");

	yield return www;

	if (string.IsNullOrEmpty(www.error)) {
		Texture2D texture = new Texture2D (www.texture.width, www.texture.height, TextureFormat.PVRTC_RGB2, false);
		www.LoadImageIntoTexture (texture as Texture2D);
		GetComponent<SpriteRenderer> ().sprite = Sprite.Create (texture, new Rect (0, 0, www.texture.width, www.texture.height), Vector2.zero);
		texture = null;
	}

	www.Dispose ();
	www = null;
}

Finally made it worked by adding Texture2D as a general variable as suggested by @saschandroid .

Also have to add Resources.UnloadUnusedAssets() at the end and the memory stay stable!

Final code, in case someone has the same issue:

public bool displayLive = false;

private Texture2D texture;

// Use this for initialization
void Start () {
	texture = new Texture2D (1,1);
}

// Update is called once per frame
void Update () {
	if(displayLive)
		StartCoroutine (DownloadImage ());
}
			
IEnumerator DownloadImage(){

	WWW www = new WWW("http://192.168.2.188/tele/image.jpg");

	yield return www;

	//Ignore the texture if it isn't at the right size
	//(Download issue)
	if (string.IsNullOrEmpty(www.error)) {
		www.LoadImageIntoTexture (texture as Texture2D);
		GetComponent<SpriteRenderer>().sprite = Sprite.Create (texture, new Rect (0, 0, www.texture.width, www.texture.height), Vector2.zero);
		DestroyImmediate(www.texture);
	}

	www.Dispose ();
	Resources.UnloadUnusedAssets();
}