Memory leaks when using GetPixels32 and SetPixels32

Hello. I’m trying to use the getpixels32 and setpixels32 methods to draw filled circles on my textures. the problem is I’m experiencing massive memory leaks when trying to do so. here’s my code.

public static void DrawFilledCircle(Texture2D texture, int x, int y, int radius, Color color)
        {

            int cx, cy, px, nx, py, ny, d;
            
            tempArray = texture.GetPixels32();
            

            for (cx = 0; cx <= radius; cx++)
            {
                d = (int)Mathf.Ceil(Mathf.Sqrt(radius * radius - cx * cx));
                for (cy = 0; cy <= d; cy++)
                {
                    px = x + cx;
                    nx = x - cx;
                    py = y + cy;
                    ny = y - cy;

                    tempArray[py * texture.width + px] = color;
                    tempArray[py * texture.width + nx] = color;
                    tempArray[ny * texture.width + px] = color;
                    tempArray[ny * texture.width + nx] = color;
                }
            }
            texture.SetPixels32(tempArray);
            texture.Apply ();

Not entirely sure how you would be getting a memory leak here just going by the code (can’t see how it’s being used, and it could well be happening elsewhere), but this kind of modification seems well suited to the GetRawTextureData function;

public static void DrawFilledCircle(Texture2D texture, int x, int y, int radius, Color32 color)
{
    int cx, cy, px, nx, py, ny, d;
             
    NativeArray<Color32> pixels = texture.GetRawTextureData<Color32>();
 
    for (cx = 0; cx <= radius; cx++)
    {
        d = (int)Mathf.Ceil(Mathf.Sqrt(radius * radius - cx * cx));
        for (cy = 0; cy <= d; cy++)
        {
            px = x + cx;
            nx = x - cx;
            py = y + cy;
            ny = y - cy;
 
            pixels[py * texture.width + px] = color;
            pixels[py * texture.width + nx] = color;
            pixels[ny * texture.width + px] = color;
            pixels[ny * texture.width + nx] = color;
        }
    }

    texture.Apply ();
}

No need to cache the texture data (and you shouldn’t because GetRawTextureData is only guaranteed to be valid at the time it is accessed) - just pay attention to image format (if the texture is ARGB32 then you may need to swizzle the Color32 to match ARGB instead of RGBA).