I’m currently struggling understanding the purpose of unique_ptr
.
I would like to have some kind of reference somewhere else of the content of std::unique_ptr<Component> component
stored inside any instance of the Object
class. Since we are using smart pointer, the raw pointer pComp
should NEVER manage memory itself because the unique_ptr
is the sole responsible for it, and this one of the core principle of Smart Pointers. However, what confuses me is that once we go out of scope and the destructor of unique_ptr
is called, which in turn free the memory, there’s no way for me to check if the memory is still valid on some other part of my code that are referencing, but not owning, to the same memory address (in this case, pComp
).
I understand that using a unique_ptr
is a very clear statement about the fact that Object
is the owner of that memory and no one else should be responsible for it, but then what’s the point of using it if the rest of my code can’t check if the memory is still valid?
Why unique_ptr
do not set their internal pointer to nullptr
? Am I misunderstanding the use of unique_ptr
? Is there a correct way to do this? If this is a case where I should use shared_ptr
, why so since I want Object to be the only responsible? Thanks to everyone in advance.
#include <iostream>
#include <memory>
class Component
{
public:
Component() { std::cout << "Component Constructor" << std::endl; }
~Component() { std::cout << "Component Destructor" << std::endl; }
};
class Object
{
public:
std::unique_ptr<Component> component;
Object() : component(std::make_unique<Component>()) { std::cout << "Object Constructor" << std::endl; }
~Object() { std::cout << "Object Destructor" << std::endl; }
};
int main()
{
Component* pComp = nullptr;
{
Object obj;
pComp = obj.component.get();
}
if (pComp == nullptr)
std::cout << "pComp is nullptrn"; // This code is never reached.
return 0;
}
Federico Dell’Aquila is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
7
In your example, pComp
has a copy of the pointer value of that stored by obj.component
. When obj
is destructed, copies of that pointer value become invalid. There is no way in C++ to test for invalid pointers, it is up to the programmer to ensure they are never accessed.
Why unique_ptr do not set their internal pointer to nullptr?
The unique_ptr
doesn’t null its underlying pointer in its destructor, because that pointer is about to cease to exist.
Is there a correct way to do this?
No, you should either extend the lifetime of obj
, or take ownership of obj.component
with your own unique_ptr
. Only if you can’t do either of those should you consider changing to shared_ptr
.
So use unique_ptr
instead of pointers. You will then get:
int main()
{
std::unique_ptr<Component> pComp;
{
Object obj;
pComp = obj.component; // compile error
}
}
It’s up to you if you want to std::move
component out of object or you have to use the objects component.
then what’s the point of using it if the rest of my code can’t check if the memory is still valid?
RAII style automatic memory deallocation.
Why unique_ptr do not set their internal pointer to nullptr?
Why would it? It’s an instruction that has to execute, that is time.
Is there a correct way to do this?
It’s up to you what you define as “correct” semantics with your object. C++ is just hammer, it’s up to where you hit.
where I should use shared_ptr, why so since I want Object to be the only responsible?
Sounds like no, bit I think you also want to make unique_ptr const so you can’t move out of it.
class Object
{
public:
const std::unique_ptr<Component> component;
...
{
Object obj;
pComp = std::move(obj.component); // also compile error
}
Than the way the only way will be to use obj.component
to get to the component. But then, you still can use .get()
and get raw pointer. It’s up to the programmer to write proper code.