Advertisement

use of shared_ptr

Started by October 29, 2017 07:15 PM
19 comments, last by ferrous 7 years, 1 month ago

Thanks for the help.

12 hours ago, BirdGB said:

f1 is the pointer for the current object selected and the foolist is the container of the all objects available. So I was a bit confused whether to delete or set null the f1 pointer after deleting or clearing the foolist.

Incidentally, this is why the whole 'foo' and 'bar' business is a bad idea. When you attempt to over-abstract a problem by removing the names, much of the useful semantic content is removed. Use meaningful names to help communicate intent.

Advertisement

Actually  I use foo just to write the example here. My names are mScene and mSceneList of class Scene. Thanks for the input and sorry for the confusion.

Why are you using shared_ptr at all? I would think that a scene list would own the scenes, and the current scene can be a [raw] pointer to one of the scenes.

 

I want to avoid memory management by myself. That's why decided to use shared_ptr.

shared_ptr alone is not a silver bullet. There are a few ways that using it can go wrong and lead to leaks and performance issues. Circular references (where you have two objects that refer to each other with a shared_ptr) are an example of that. Mitigating cases like that constitutes "memory management" to some degree.

If you're working in C++ you are going to have to deal with memory management even if you're using smart pointers, mainly in the form of ownership semantics.

Now, shared_ptr is intended to be used in cases where ownership is shared, hence the name. Is shared ownership really what you want here? If only one object (the scene list) will ever have ownership of the scenes, then you can represent that in code using a unique_ptr, is a smart pointer template that enforces the idea that only one object at a time can own the memory that it points to. You can use references or raw pointers if you need objects to refer to memory they don't own and unique_ptrs to refer to the memory if the objects do own.

Advertisement

I want only one ownership, that is the scenelist, so that I don't need to bother about other pointers and delete the scenelist when required. But I still need another single pointer that point to any of the element from the scenelist at a time.

I have only one question now, a unique_ptr will be deleted automatically even if I forget to delete it myself?

7 minutes ago, BirdGB said:

I want only one ownership, that is the scenelist, so that I don't need to bother about other pointers and delete the scenelist when required. But I still need another single pointer that point to any of the element from the scenelist at a time.

I have only one question now, a unique_ptr will be deleted automatically even if I forget to delete it myself?

That is part of the purpose of unique_ptr.

Ok, thanks for the detail explanation and help.

Also, in addition to possibly using a raw pointer, you could use an index.

then its sceneList & currSceneIndex;

 

This topic is closed to new replies.

Advertisement