Mesh.dispose() generates an error when called from pointerDragBehavior.onDragEndObservable

Hi all,

I have a scene in which i want to remove a meshed once it has been dragged onto another mesh. I use the pointerDragBehavior.onDragEndObservable and call mesh.dipose() which seems to generate some kind of recursion error in Firefox and Chrome.

PG to reproduce: https://www.babylonjs-playground.com/#YEZPVT#61

Odd :slight_smile:
but unrelated to mesh.dispose(), seems to also happen when not disposing.

behavior.attach(node) seems to work.
https://www.babylonjs-playground.com/#YEZPVT#66

This is a bit of an edge case.

When you dispose the sphere, the behaviour is also disposed. Because you are in a dragging state, the onDragEndObservable observable is called in the dispose method, leading to the recursive calls.

You can either dispose the sphere outside of the onDragEndObservable observable or set pointerDragBehavior.dragging = false; just before calling sphere.dispose():

https://www.babylonjs-playground.com/#YEZPVT#67

1 Like

I see, thanks for explaining. That workaround should do it for me. Although it seems a bit counterintuitive to me that it’s still in the dragging state when the onDragEndObservable is called.

In fact the same releaseDrag function is called when a drag ends and when disposing of the behaviour. And this is the function that checks the state of dragging and iftrue raises the onDragEnd observable.

The system can’t know when you call dispose, so can’t set dragging = false before calling releaseDrag in the dispose method.

Thanks again for explainig and sorry for nitpicking, but couldn’t the releaseDrag function set dragging = false before raising the onDragEnd observable? Don’t get me wrong, I’m fine with the workaround of setting it on my own I’m just under the impression that would make the behavior more intuitive. But since I haven’t looked into the code I’m most likely missing something.

No you’re right and I’m stupid:

3 Likes

:wink:
That was fast, thanks a lot!