1

The best example I can give is with the Three.js library in javascript :

function createMesh(){
   var geometry = new THREE.SphereGeometry(10, 5, 5);
   var material = new THREE.MeshPhongMaterial();
   var mesh = new Mesh(geometry, material);

   return mesh;
}

This translates in C++ to :

Mesh* createMesh(){
   SphereGeometry* geometry = new SphereGeometry(10, 5, 5);
   MeshPhongMaterial* material = new MeshPhongMaterial;
   Mesh* mesh = new Mesh(geometry, material);

   return mesh;
}

Now, who should have the responsability to delete these 3 pointers. Should it be the user of the library? Or should the geometry and the material be destroyed in the destructor of Mesh, and the user should just delete the Mesh pointer? And if this is the case, how will the player know to not delete his pointers and avoid a double deletion?

Also, what if this happens :

SphereGeometry geometry(10, 5, 5);
MeshPhongMaterial material;
Mesh* mesh = new Mesh(&geometry, &material);

Thanks.

8
  • 3
    Use smart pointer, and you no longer have those questions.
    – Jarod42
    Jul 10, 2017 at 21:41
  • 2
    BTW, it is more natural that the one which allocates is the one which deallocates.
    – Jarod42
    Jul 10, 2017 at 21:42
  • 2
    " But wouldn't this force the user of the library to use smart pointers?" No.
    – user2100815
    Jul 10, 2017 at 21:45
  • 7
    Why are these objects being allocated dynamically at all? Why not just use their values? Jul 10, 2017 at 21:46
  • 1
    If the library provides a unique_ptr the user has the complete choice to release the raw pointer or move it into a shared pointer (or any other kind of smart manager). Therefore unique_ptr should be the preferred choice.
    – Galik
    Jul 10, 2017 at 21:48

2 Answers 2

9

With smart pointer, ownership is clear:

std::unique_ptr<Mesh> createMesh(){
   auto geometry = std::make_unique<SphereGeometry>(10, 5, 5);
   auto material = std::make_unique<MeshPhongMaterial>();
   return std::make_unique<Mesh>(std::move(geometry), std::move(material));
}
6

Should it be the user of the library?

The library lacks reliable information on when to delete these objects, so it must be the user. He does not need to do it manually, though (see the point at the end regarding smart pointers).

Or should the geometry and the material be destroyed in the destructor of Mesh, and the user should just delete the Mesh pointer?

Transferring ownership in a constructor is a viable alternative. It also lets you "fold" the code into something shorter, which would not be possible with separate deletions:

Mesh* mesh = new Mesh(new SphereGeometry(10, 5, 5), new MeshPhongMaterial);

And if this is the case, how will the player know to not delete his pointers and avoid a double deletion?

By reading documentation.

Also, what if this happens :

Mesh* mesh = new Mesh(&geometry, &material);

As long as you do not make mesh available outside the scope of geometry and material, nothing happens.

A better approach is to avoid using raw pointers, if at all possible. Use objects instead of pointers to objects everywhere where it is practical. Use collections when dynamic sizing is needed. Use smart pointers when you must have a pointer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.