Replies: 2 comments 3 replies
-
|
When using Box2D as a shared library you would need Box2D to both allocate and free the userData memory, or you'll get an access violation or segfault. In this context Box2D cannot free the pointer, and it would need to store a flag to know that it was the origin of the pointer. It's also up to you how you allocate and free the userData for your usecase. It could live longer than the body, or shorter (you can swap it for another object anytime with b2Body_SetUserData, or use the pointer to store a number, or set it to 0). I would suggest that allocating and freeing memory is not the responsibility of a physics engine, and adding the capability to Box2D unnecessarily adds complication and ambiguity. You should be keeping track of your Box2D bodies already, if you don't want the userData memory for longer than the body, then it's up to you to free it when you delete the body. |
Beta Was this translation helpful? Give feedback.
-
|
Part of the design goal of v3 has been to remove as many callbacks as possible. Every callback creates a new failure point. For example, Box2D could callback when a body is being destroyed and the user can destroy all the shapes on the body, leaving the body destroy function with some potentially stale references. My approach for v3 has been to only have callbacks when there is no other reasonable choice. Here are the current callbacks:
Some things that were handled with callbacks in v2 are now handled with events, such as begin/end touch events. Events are safer and more efficient. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
More often than not, userData stored in worlds, bodies and shapes will be allocated on a per-object basis, requiring users to manually deallocate them for each object to avoid memory leaks.
For example, whenever I want to destroy a body, I need to iterate over all of its shapes, get each userData and delete them manually. After that, I get the body's userData and manually delete it as well.
Of course this could be wrapped in a helper function, but it would be much nicer if users could provide a userData deleter callback that Box2D would call whenever those objects are about to be destroyed. Of course, we would need one more function pointer in each struct and probably a getter/setter function for the deleter itself as well.
Beta Was this translation helpful? Give feedback.
All reactions