InvalidOperationException: Cannot modify VisualElement hierarchy during layout calculation

For the same process (enabling a UI Document and setting some values (like cleaning up some containers, removing specific Visual elements and inserting newly created Visual elements on them) I’m getting 4 different exceptions/errors.

When it happens: Normally this process works just fine both in the editor and mobile; however, recently we added a new feature that clicking on a mobile notification directs the player to a specific page (of course after async operations that take more than seconds). Clicking on a in-game button does the same but we are facing this issue only after clicking the notification; so we are suspecting that it may cause the layout calculation to be stuck at some point.

Where it happens: In the editor and some iPhone devices (We were able to test it in different iPhone 11s and 14 Pro) it was just fine, but we tested it in two Android devices (Redmi Note 12 Pro and Redmi Note 8 Pro) It has thrown the errors listed below.

Exception 1:

InvalidOperationException: VisualElements cannot be added to an active visual tree during generateVisualContent callback execution nor during visual tree rendering

UnityEngine.UIElements.UIR.RenderChain.UIEOnChildAdded (UnityEngine.UIElements.VisualElement ve)
UnityEngine.UlElements.VisualElement+Hierarchy.Insert (System.Int32 index, UnityEngine.UIElements.VisualElement child)
UnityEngine.UIElements.VisualElement.insert (System.Int32 index, UnityEngine.UIElements.VisualElement element)

Exception 2:

InvalidOperationException: Cannot modify VisualElement hierarchy during layout calculation

Error 3:

Assertion failed
9857115--1419693--upload_2024-5-27_13-0-23.png

Error 4 and 5 actually:

Trying to destroy object that is already released to pool

9857115--1419696--upload_2024-5-27_13-2-8.png

9857115--1419705--upload_2024-5-27_13-12-11.png

My questions:

  1. I’m adding my child Visual Elements by using Insert() method in the same frame (Because I want a specific element to be the latest child). Would it be better if I just use Add() and then set the sibling index of that element afterwards? Would it make the calculation faster?

  2. What if I divide the operation in different frames using coroutines? Would it solve my problem?

  3. Could clicking on the notification have something to do with it? I’m curious why it does not happen normally but happens after clicking the notification.

  4. Does this happen because of hardware limitations? I want to know why we could not reproduce it on iOS devices.

  5. Is this a bug or a known limitation of UI Toolkit. If so, why it is not clearly stated in any documentation?

Unity Version: 2021.3.35f1

Thank you in advance for your help.

The first exception is pretty clear and you need to solve that. When generating the meshes, you should not change the hierarchy in that callback. Reporting a few more line of the first error would help pinpointing the source…

Also all api need to be called from the main thread, but it seems you are already doing that.

How can I know that it is done generating the meshes? The same scenario results in an exception in Android devices, however it is not even an issue with iPhone devices. The only thing I do is to Insert an element to hierarchy. How could something that is working pretty well for ages be a problem now?

But you don’t tell me how? And what about the other errors?