How to protect smart pointer’s address from scope lifespan in c++

I am working on a C++ code in which I deal with shared_ptr<Toto> (where Toto is an arbitrary object); let’s say a.
And for some technical reasons, I also have to store the address of these pointers for extra manipulation somewhere else, so I have a std::shared_ptr<std::shared_ptr<Toto>> which is the address attribute of a class addressBook.
Before, you start bashing me, I know, such pointer of pointer is in most of the case a sign of a bad architecture ; However in this particular case, my coworker and I arrived to the conclusion that was this only way for this code. Otherwise, you can see it as an exercise in style, and it does not change the nature of the question.

The problem is that a (std::shared_ptr<Toto>) and book.address (std::shared_ptr<std::shared_ptr<Toto>>) are not necessarily declared in the same scope, so don’t have the same life-span.
With this difference of scope, I can access to the address and the value of a (std::shared_ptr<Toto>) after its destruction through book.address (std::shared_ptr<std::shared_ptr<Toto>>), so this can lead to unsafe situations.

I came up with a workaround using an additional watcher named tmp_proxy (std::shared_ptr<std::shared_ptr<Toto>>) containing a deleter custom_deleter and the address of the std::shared_ptr<Toto>& sptr that I need to declare in the scope, so they should have the same life-span.
But I am sure that is safe and I found it cumbersome.

Here is a MWE:

#include <iostream>
#include <memory>

class Toto {
public:
    double hello;

    ~Toto() {
        std::cout << "Destructor Toto with value " << hello << std::endl;
    }
};

int status = 0; // global variable

void custom_deleter(std::shared_ptr<Toto>* ptr) {
    std::cout << "WOW WOW I should make sure that "<< (ptr).get()->hello << " at " << (ptr) <<" is not accessible anymore !" << std::endl;
    status += 1; // Increment status
    //delete ptr;  // Actually delete the object
};

class addressBook {
public:
    std::shared_ptr<std::shared_ptr<Toto>> address;
    
    std::shared_ptr<std::shared_ptr<Toto>> add_watcher(std::shared_ptr<Toto>& sptr) {
         std::cout << "add watcher" << std::endl;
         std::shared_ptr<std::shared_ptr<Toto>> a_watch_addr(&sptr, custom_deleter);
         //address = a_watch_addr;
         auto nullDeleter = [](void*) {};
         address.reset(&sptr, nullDeleter);
         return a_watch_addr;
    };
    
    ~addressBook() {
        std::cout << "Destructor addressBook with address " << address << std::endl;
    };
};

int main() {
    { // First scope
        std::shared_ptr<Toto> b = std::make_shared<Toto>();
        addressBook book;
        b->hello = 3.14;
        { // Opening second scope
            std::shared_ptr<Toto> a = std::make_shared<Toto>();
            a->hello = 666.666;
            std::shared_ptr<Toto> c = std::make_shared<Toto>();
            c->hello = 111.2;
            std::cout << "at init: a=" <<a << " ; &a=" << &a <<" and c=" <<c << " ; &c=" << &c  << std::endl;

            std::cout << "first a receives a new_deleter" << std::endl;
            auto tmp_proxy= book.add_watcher(a);

            std::cout << "and a is redirected to point to c" << std::endl;
            a = c; // `custom_deleter` is not called here

            std::cout << "a & c will be destroyed by end of scope =>Wow should appear AFTER this point" << std::endl;
        } // Closing second scope
        std::cout << "Book address "<< book.address << " *address "<< *book.address.get() << " *address->value "<< (*(book.address.get()))->hello << std::endl;
        std::cout << "now b and Book will be destroyed:" << std::endl;
    } // Closing first scope -> b is out of scope
    std::cout << "b and Book got destroyed" << std::endl;
    std::cout << "END MAIN with status " << status << std::endl;
    return 0;
}

and here is the output:

at init: a=0xeea2e0 ; &a=0x7fffd54c22f0 and c=0xeea300 ; &c=0x7fffd54c22e0
first a receives a new_deleter
add watcher
and a is redirected to point to c
Destructor Toto with value 666.666
a & c will be destroyed by end of scope =>Wow should appear AFTER this point
WOW WOW I should make sure that 111.2 at 0x7fffd54c22f0 is not accessible anymore !
Destructor Toto with value 111.2
Book address 0x7fffd54c22f0 *address 0xeea300 *address->value 111.2
now b and Book will be destroyed:
Destructor addressBook with address 0x7fffd54c22f0
Destructor Toto with value 3.14
b and Book got destroyed
END MAIN with status 1

So my questions are:

  • Is it possible to observe the address of <std::shared_ptr<Toto> a (and not the address of content) to determine when it is destroyed ?
    This in order to execute a function at that moment to make sure I will not try to access this address anymore.
  • Is there any better way to do it, that my ugly idea ? if not really, is it safe ?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật