Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Examples/Vehicle] Fix Ammo null function error on restart #6902

Merged
merged 4 commits into from
Sep 4, 2024

Conversation

kungfooman
Copy link
Collaborator

@kungfooman kungfooman commented Sep 2, 2024

Fixes #6724

These are just my current findings, the engine still seems to leak rigidbody Ammo objects/pointers... but at least this makes an Example refresh work in the first place. This allows for further fixes without just crashing like it currently does here: https://playcanvas.vercel.app/#/physics/vehicle

About 7b0c745 / Removing Ammo.destroy(origin);:

The origin is part of btTransform:

https://github.com/kripken/ammo.js/blob/1ed8b58c7058a5f697f2642ceef8ee20fdd55e10/bullet/src/LinearMath/btTransform.h#L32-L41

You don't just get that pointer and call free on it - at least it doesn't make sense to me. Probably at best nothing happens and worst case is messing up internal allocation structures while causing other subtle bugs. Any other opinions on this?

Feedback about all this highly welcome and some testing.

I can't even logically explain why the null function error occurs, since why should it matter if we reuse some old allocated pointers or free them and recreate those anew - it feels like a black box. 🙈

Vercel quick link: https://engine-r0b3y912i-playcanvas.vercel.app/#/physics/vehicle

I confirm I have read the contributing guidelines and signed the Contributor License Agreement.

@kungfooman
Copy link
Collaborator Author

I wanted to keep track of every object and then it turned out that it's not even possible to destroy btVehicleTuning because of:

https://github.com/kripken/ammo.js/blob/1ed8b58c7058a5f697f2642ceef8ee20fdd55e10/ammo.idl#L936-L945

So I just deleted the NoDelete keyword, rebuild Ammo.js via cmake/emcc and Ammo.destroy'ed the vehicle tuning class aswell just like the others - turns out I can refresh the example more than 100x without one strange occurance of twisted or detached tires.

@LeXXik
Copy link
Contributor

LeXXik commented Sep 3, 2024

So I just deleted the NoDelete keyword ... and Ammo.destroy'ed the vehicle tuning class ...

Hmm, I might be not correct here, but I don't think Emscripten actually destroys it. WebIDL "nodelete" keyword means that this class cannot be deleted, since it has no public destructor (e.g. it is private). I think ems simply suppresses the error, when you try to destroy it. Similar to how you won't get an error, if you try to destroy an object a second time:

const vec = new Ammo.btVector3(0, 0, 0);
Ammo.destroy(vec);
Ammo.destroy(vec); // no error

Using arrow function

Co-authored-by: Will Eastcott <[email protected]>
@kungfooman
Copy link
Collaborator Author

kungfooman commented Sep 4, 2024

@LeXXik Yea, that description about no-destructor I read aswell and it makes absolutely no sense to me. You don't need a destructor to free memory, it just feels like an arbitrary and even harmful limitation. Historically classes were basically "structs with functions" and in order to have a struct, you have to allocate its memory... and then obviously free that again for good housekeeping - which that keyword makes impossible.

We can discuss this out further in a new issue/PR, in this one I'm just happy to have the actual bug fixed.

@willeastcott
Copy link
Contributor

We can discuss this out further in a new issue/PR, in this one I'm just happy to have the actual bug fixed.

So folks are happy for me to go ahead and merge this now?

@willeastcott willeastcott added bug area: physics Physics related issue labels Sep 4, 2024
@LeXXik
Copy link
Contributor

LeXXik commented Sep 4, 2024

So folks are happy for me to go ahead and merge this now?

Yes, this is a good PR, regardless of the bug.

@willeastcott willeastcott merged commit 94267ac into playcanvas:main Sep 4, 2024
8 checks passed
@kungfooman kungfooman deleted the fixAmmoNullFunction branch September 4, 2024 10:30
@LeXXik LeXXik mentioned this pull request Sep 8, 2024
@LeXXik
Copy link
Contributor

LeXXik commented Sep 20, 2024

@willeastcott suggesting this for v1

willeastcott added a commit that referenced this pull request Sep 20, 2024
* Just example simplification (forEach -> map)

* Don't call `Ammo.destroy` on a internal transform pointer

* Clean up temporary Ammo objects on restart

* Update examples/src/examples/physics/vehicle.example.mjs

Using arrow function

Co-authored-by: Will Eastcott <[email protected]>

---------

Co-authored-by: Will Eastcott <[email protected]>
@willeastcott
Copy link
Contributor

@LeXXik Done. I've just cherry picked it to main_v1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: physics Physics related issue bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Engine-only example crash
3 participants