GithubHelp home page GithubHelp logo

Comments (1)

DanielChappuis avatar DanielChappuis commented on September 26, 2024

Hello,

The rule is the following one for cleaning up allocated objects : If you have allocated memory by yourself (using the new operator), you need to clean this memory by yourself (with the delete operator). Otherwise, the memory has been allocated by the physics engine and will be released by the physics engine.

You can find an example of the creation of a concave mesh shape in the testbed application here :
https://github.com/DanielChappuis/reactphysics3d/blob/master/testbed/common/ConcaveMesh.cpp

In this example you can see that I create some TriangleVertexArray as in the following code :

// For each subpart of the mesh
for (unsigned int i=0; i<getNbParts(); i++) {

   // Vertex and Indices array for the triangle mesh
   rp3d::TriangleVertexArray* vertexArray = new rp3d::TriangleVertexArray(.....);

   // Add the triangle vertex array of the subpart to the triangle mesh
   triangleMesh.addSubpart(vertexArray);
}

As you can see, I allocate memory by myself with the "new" operator. Therefore, I am responsible to delete this memory. I do it with the following code :

// Destroy the triangle vertex arrays
for (unsigned int i=0; i<triangleMesh.getNbSubparts(); i++) {
    delete triangleMesh.getSubpart(i);
}

I also create the ConcaveMeshShape with the "new" operator as follows :

// Create the collision shape
concaveShape = new rp3d::ConcaveMeshShape(&triangleMesh);

Therefore I have to delete this shape at the end like this :

delete concaveShape;

Note that when you add a collision shape to a body, you get back a pointer to the proxy shape as you can see with this code :

// Add a collision shape to the body
proxyShape = body->addCollisionShape(concaveShape, rp3d::Transform::identity(), mass);

As you can see, you have not allocated memory for the proxy shape by yourself here (no new operator). This memory has been allocated by the physics engine. Therefore, you do not need to delete this object by yourself. The proxy shape will be deleted by the physics engine when you remove the proxy shape from the body or you destroy the body.

I hope this helps.

from reactphysics3d.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.