BatchRendererGroup random crash when adding new render object with material properties changed

When using the batchrenderergroup on a mobile device, I encountered an issue that could cause the application to freeze and eventually crash. It happens on 2022.3.0 - 2022.3.14 URP

void Start()
{
    m_BatchRendererGroup = new BatchRendererGroup(OnPerformCulling, IntPtr.Zero);
    return;
}

void Update()
{
//Test code for adding new cube
    for (int i = 0; i < 200; i++)
    {
        var obj = Instantiate(cube,transform);
    }
}

public JobHandle OnPerformCulling(BatchRendererGroup rendererGroup, BatchCullingContext cullingContext, BatchCullingOutput cullingOutput, IntPtr userContext)
{
    return new JobHandle();
}

After create a BatchRendererGroup, all you need is keep adding new render object (such as cubes) to the scene. The application will freeze and then crash after reaching a certain quantity.
The probability of crash depends on device, the better the lower, so sometimes app will die lack of memory before freeze. But it do occur on almost every device we tested.

This is the stack just before crash.


I wonder if this is a bug or it is not allowed to use batchrenderergroup this way.
Please reply to me asap, thx.
PS. sorry for the pool English…

Hello.

It seems like you are not using the BatchRendererGroup the way it was meant to be used. It is not meant to be used with GameObjects directly and instead you’re supposed to upload all the data and prepare the draw commands yourself. Please refer to this article: https://blog.unity.com/engine-platform/batchrenderergroup-sample-high-frame-rate-on-budget-devices

In your code, it seems you’re instantiating 200 cubes on every update but aren’t ever destroying them so they add up and eventually cause the runtime to crash because of memory constraints.

Thank you for the reply. This code is just a demo to show how to reproduce crash. Original project is quite large. In fact, I use brg to draw static scenes with self prepared data , I also baked my own occ data and added complex logic in OnPerformCulling funciton. But when I together add some dynamic objects like characters use triditional way, crash occurs. It took me quite a long time to finally locate the problem. It seems that once m_BatchRendererGroup = new BatchRendererGroup(OnPerformCulling, IntPtr.Zero);
is used,it will crash after you add game object to the scene.
And I can tell difference between my issue and memory constraints.Its just a demo.

By the way ,if you dispose BatchRendererGroup obj and recreate it every single frame, crash can be avoided. While its not a good solution, it make me doubt if there is some unexcepted mem access ?

When this issue occurs, it first freeze the screen for a longtime, meanwhile memory begin to increase, finally app crash for lack of memory

You’re not supposed to create and dispose BRGs every frame. While that’s a valid code path, it’s definitely not what is intended.
I’m afraid I have you all the feedback I could given the code snippet you pasted. If you need more support you’re going to have to create an example where the issue reproduces. Just last week I made a demo with game objects and BRG rendering and it does work well with the latest 2022 version.

Problem solved. When adding new object, change material property will cause crash.

using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour
{
    public GameObject cube;
    private Queue<GameObject> q;

    private const int cap = 20000;

    void Start()
    {
        q = new Queue<GameObject>(cap);
        for (int i = 0; i < cap; i++)
        {
            var obj = Instantiate(cube,transform);
            obj.transform.position = cube.transform.position + new Vector3(Random.Range(-5f, 5f),Random.Range(-5f, 5f), Random.Range(-5f, 5f));
            q.Enqueue(obj);
        }
    }

    // Update is called once per frame
    void Update()
    {
        for (int i = 0; i < 200; i++)
        {
            DestroyImmediate(q.Dequeue());
            var obj = Instantiate(cube,transform);
            obj.transform.position = cube.transform.position + new Vector3(Random.Range(-5f, 5f),Random.Range(-5f, 5f), Random.Range(-5f, 5f));
            q.Enqueue(obj);
        }
    }
}
using UnityEngine;

// add this script to cube
public class testMat : MonoBehaviour
{
    private Material m;
    private MeshRenderer mr;
    private int id = Shader.PropertyToID("_BaseColor");
    private MaterialPropertyBlock mpb;

    void Start()
    {
        // Crash
        m = GetComponent<MeshRenderer>().material;
        m.SetColor(id,Color.blue);
     
        // Crash again
        //mr = GetComponent<MeshRenderer>();
        //mpb = new MaterialPropertyBlock();
        //mr.GetPropertyBlock(mpb);
        //mpb.SetColor(id,Color.blue);
        //mr.SetPropertyBlock(mpb);
    }
}

Once you use m_BatchRendererGroup = new BatchRendererGroup(OnPerformCulling, IntPtr.Zero) in your proj, crash occurs with test code. Any idea?
I tested mpb instead, but still crash. Wonder if this is a bug or change mat properties is not allowed to use with brg?
PS, try build apk with development build , crash probability is low with a release build.

Test mpb again, change mpb to static is ok, with mpb create once, crash prob is close to zero. I will use this solution for now,