0
votes

I wish to use a smart pointer to an object that will be accessed by quite a few different objects. Therefore i don't want to destruct it until all the pointers are released to it. The smart pointer will point to an object that instantiates a couple objects, which instantiate a couple objects, and so on, so that you can access all the individual System objects through this one pointer. However, let's say that i create the smart pointer in main as std::shared_ptr<System> object(new System); and then let's say i have another object that all other entities derive from which has a static std::shared_ptr that i set equal to object above. The static declaration means that it's managed by the compiler and not on a per instance basis. So let's say that all the objects that derive from the base class use this static pointer to access the System object. WHEN is this smart pointer destructed if ever? I need this pointer to be destructed before the one in main, who's destruction should drop the reference count to 0, destroying the object pointed to by it. So my question is, when is a statically declared std::shared_ptr destructed in the scheme of things. Or even if a pointer were declared globally and used all over, when would this object be destructed?

the base class would look something like this:

class Base
{
public:
    static shared_ptr<System> m_System;
};

int main()
{
    std::shared_ptr<System> system(new System);
    Base::m_System = system;

    /*
        other stuff
    */ 
    return 0;
}

also another thing that someone could help me with, i'm not very good with const-ness, and i would like for that static pointer to be a constant that cannot be changed by the derived classes. But then if it's const i can't initialize it like i did in main, right? so how would i go about declaring it so that it is const, but i can still set it equal to a pointer initially like this.

2
shared_ptrs are not magical. They are just objects like any other. The rules are the same as for other objects. - R. Martinho Fernandes

2 Answers

1
votes

We have 2 reference for object, so, when we exit from main function - we decrement reference counter and then destruction of static objects decrement reference counter, when reference counter will be equal to 0, our System object will be destroyed.

1
votes

WHEN is this smart pointer destructed if ever?

Like all static objects, it's destroyed after main exits.

I need this pointer to be destructed before the one in main, who's destruction should drop the reference count to 0, destroying the object pointed to by it.

In that case, perhaps you want the static pointer to be a weak_ptr. It won't participate in the reference-counting that keeps the object alive, but will allow access to the object as long as there is a shared pointer to it. Then the object will be destroyed once the the pointer in main() (and any other copies of that pointer) go out of scope.

Alternatively, it might be worth thinking about avoiding static objects, and instead instantiate the object within main() and pass references to whatever needs it. This avoids various problems associated with static objects (in particular, their initialisation order), and also makes the program's dependency structure clearer. In that case, you probably don't need a smart pointer at all, since the object lifetime is bound to that of the program.