I’ve got a problem with memory leaking on PC, using C#. I’m using the WWW class, but not using the “yield return” methodology since it seem not to work.
After loading an image, it is set as texture to GUI label.
This code properly loads an image from the web, but if I repeat the action the old image(s) stays in memory and won’t go away, even if I don’t use it in the code anymore.
public class GuiApp : MonoBehaviour
{
public string ImageUrl = "http://some.image.url";
public Rect RenderingRect;
private WWW _request;
Start(){
_request = new WWW(ImageUrl);
}
Update(){
if (_request.isDone) {
// draw label with texture
GUIContent content = new GUIContent();
content.image = _request.texture;
GUI.Label(RenderingRect, content);
// dispose WWW
_request.Dispose();
}
}
}
So, is this a problem in my approach, a problem with Unity or with Mono?
It does work fine, actually, and using Update like that isn’t very efficient since you’re constantly polling values when it’s not necessary.
Anyway, as the docs say about WWW.texture: “Each invocation of texture property allocates a new Texture2D. If you continously download textures you must use LoadImageIntoTexture or Destroy the previously created texture.”