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 ?