Better Rigidbody and Constraint Memory Management #101877
Labels
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#101877
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Here is a general diagram of the relevant pieces of the
Object
andRigidBodyWorld
modelsProblem Scenarios:
Scenario 1.A:
Empty - Has constraint.
Object1 - First object in constraint
Object2 - Second object in constraint.
Play simulation for a few frames. Pause simulation. Delete Object2 from scene.
Test1a.blend
Problem
For Object2, its btRigidBody has a pointer to the btTypedConstraint. When deleting the btRigidBody, we tell the btTypedConstraint to
removeConstraintRef()
.This however just marks Object2's btRigidBody as not colliding with Object1's btRigidBody, it does not remove the btRigidBody from the btTypedConstraint.
The memory for Object2's btRigidBody gets deleted, and btTypedConstraint now contains junk memory that crashes when attempting to run the simulation for the scene RigidBodyWorld.
From looking at the header of btTypedConstraint, it seems like the intent is to never swap out the rigid bodies or change them after the creation of the constraint.
So we need to ensure that when an object gets removed from the scene, the btTypedConstraint gets removed from the btDiscreteDynamicsWorld for the scene.
(This also happens when both objects are set in the rigidbody constraint, the simulation is played for a few frames and then paused, and then UNDO takes the object out of the constraint.)
Scenario 1.B:
Empty - Has constraint.
Object1 - First object in constraint
Object2 - Second object in constraint.
Play simulation for a few frames. Pause simulation. Delete Empty from scene.
Didn't cause any crashes that I could find
Scenario 2.A:
Object1 - Has constraint.
Object2 - Second object in constraint.
Play simulation for a few frames. Pause simulation. Delete Object2 from scene.
Problem:
Essentially the same as 1.A
Scenario 2.B:
Object1 - Has constraint.
Object2 - Second object in constraint.
Play simulation for a few frames. Pause simulation. Hit Ctrl-Z to undo and move back to start of simulation.
Test2b.blend
When hitting UNDO, we iterate throught the objects in the scene to delete and recreate them in the previous state.
When deleting, if Object1 is deleted first, that has the constraint on it, we delete the physics object and the constraint.
Each btRigidBody contains an array of pointers to constraints that it is in. When deleting the physics body, we need to ensure that it is removed from all constraints.
We remove the constraint from Object1's btRigidBody, and then delete the btTypedConstraint.
However Object2's btRigidBody still has a pointer to the btTypedConstraint. When deleting the btRigidBody, we attempt to remove it from the btTypedConstraint,
but the memory has already been freed, so we get a crash.
These are the most obvious issues with the current implementation, though it is not exhaustive. There are further problems with memory getting corrupted between undoing and rebuilding the simulation that I could not pin down so specifically, but I think these are representative of what the issues boil down to.
The crux of the issue is that when deleting any physics object, it needs to know about all the constraints it is in and all the other objects that are related to it in the physics simulation. And if it is in a constraint, but doesn't actually own the constraint, there's no way to find the other objects and relationships it cares about. This also leads to the order-of-processing issues depending on which objects get deleted first when undoing/rebuilding a simulation.
Resolution Ideas
My suggestion is to no longer free
ob->rigidbody_object
orob->rigidbody_constraint
directly from anObject
.The Scene's
RigidBodyWorld
should be responsible for handling removal of all rigidbody related data.Object
->Set<RigidBodyCon>
so when any Object is being removed the RBW has a link to any related constraints. An object could be in multiple constraints, hence theSet<RigidBodyCon>
. In theory we could loop through all constraints in the scene to find which constraints an object is in, but this would obviously have a performance impact on a scene with a lot of constraints.Create a hashmap on the
RigidBodyWorld
data structure to contain a map of Objects to the constraints they are in.When rigidbodies or constraints are going to be added to/removed from the scene, ensure that the RigidBodyWorld is responsible for deleting all relevant physics objects and constraints, rather than deleting them from the Object directly.
For Scenario 1.A/2.A: the RBW would ensure that if a
btRigidBody
is removed from abtTypedConstraint
, thebtTypedConstraint
should get removed from thebtDiscreteDynamicsWorld
.For Scenario 2.B: the RBW would ensure that when deleting a constraint, all objects in the constraint have the
btTypedConstraint
removed from theirbtRigidBody
.Added subscribers: @SonnyCampbell_Unity, @lichtwerk, @vinegarshots, @PratikPB2123, @mod_moder, @ZedDB
Do we have any progress or plan solving this issue?
I think this should have a high priority...