After just a few minutes my app crashes. I assume the issue is in the following script. If, as I think, the issue is in the LateUpdate method where I create a new Texture2D and new Rect object, how do I accomodate changing screen sizes (this is a standalone application)? Code (csharp): using UnityEngine; using System.Collections; public class ReadPixelsReduced : MonoBehaviour { private int _sW; private int _sH; float originX; float originY; [SerializeField] int width=32; [SerializeField] int height=28; Rect rect; Rect testRect; Texture2D texture; private Texture2D test_Texture; float [] greyscaleVals = new float [32*18]; // Use this for initialization void Start () { } void LateUpdate(){ _sW=Screen.width; _sH=Screen.height; originX = 0.5f * _sW - (.5f * width); originY = 0.5f * _sH - (.5f * height); rect = new Rect(originX,originY, width, height); texture = new Texture2D(_sW,_sH,TextureFormat.RGB24,false); } void OnPostRender(){ } void OnRenderImage (RenderTexture source, RenderTexture destination) { texture.ReadPixels(rect,0,0,false); texture.Apply(); Graphics.Blit (source, destination); int x, y = -1; for (int i = 0; i < 32 * 18; i++) { x = i % 32; greyscaleVals = texture.GetPixel(x, x == 0 ? ++y : y).grayscale; } Debug.Log("ASSERT OnRenderImage greyscaleVals.Length ] = "+greyscaleVals.Length+"\n"); Debug.Log("ASSERT OnRenderImage greyscaleVals.Length[0][1] = "+greyscaleVals[300]+"\n"); } void OnGUI () { } }
Use code tags, please: http://forum.unity3d.com/threads/using-code-tags-properly.143875/ Also, why are you creating a new Texture2D EVERY update? I could see having to if you want to match the screen resolution exactly... but in that case only create a new texture IF and ONLY IF the screen has actually changed in size. And if you're going to be replacing a Texture with another Texture, as with any unmanaged object (Texture2D is the managed mono object that represents the unmanaged data on the unity side of things), you need to manually destroy it by calling Object.Destroy(...). The same way you would with a Component, GameObject, or other unity Object.
Thanks. Added CODE tags. Moved new Texture2D to my Start method. Do you know if creating a new Rect every frame could also potentially cause memory issues?
I think creating a new rect would not cause any memory issues, since im pretty sure Rect is a value type that is on the Stack.
Rect is a struct, so it's a value type. It is not an unmanaged unity object type, so you don't have the leaking issues with that. You'll know if it's a unity if it inherits from UnityEngine.Object. Being a value type doesn't necessarily mean it'll be on the stack. In this instance, because said rect is a class member field, it'll exist as part of the memory that is allocated when you instantiate that class. Which is on the heap. Structs being "on the stack" is that when used as a temporary value in a short scope (a scope of code that closes soon, like a function, if statement, for loop, etc) a value will be placed on the stack. Where as an object created in this scope will be on the heap, and the pointer to it on the stack.