Confusion regarding lifetimes #1369
-
(I'm using v5.2.0) I have some code that creates a sphere shape, then uses it in creating one or more bodies. There are two ways of creating a sphere shape as far as I can tell. 1: auto sphereSettings = JPH::SphereShapeSettings(1);
sphereSettings.SetEmbedded();
auto sphere = sphereSettings.Create().Get(); // `sphere` is Ref<Shape> 2: auto sphere = JPH::SphereShape(1); // `sphere` is on the stack However, when The assert no longer trips if I I guess I have two main questions:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You cannot create a shape on the stack if you want to attach that shape to a body. The body doesn't make a copy of the shape, it takes a reference. So unless you create and destroy a body within the scope where That said, you can do things like:
because then
|
Beta Was this translation helpful? Give feedback.
You cannot create a shape on the stack if you want to attach that shape to a body. The body doesn't make a copy of the shape, it takes a reference. So unless you create and destroy a body within the scope where
sphere
exists, you'll get crashes.That said, you can do things like:
because then
sphere
lives long enough for the narrow phase query to be done with it and it is more efficient than: