The described use case would be the following, right?

 

1. an object is allocated on the heap pointed to by p1

2. a function f is called copying (with arguments) the pointer to p2

3. during the run of the function f, f calls some other code; there p1 is freed (ideally p2 is also set to nullptr)

4. the function f tests p2 for nullptr, if == 0, skip 5

5. f dereferences p2 one or multiple times

 

 

The solution described would work, if step 3 does not happen between the test in 4 and the dereferencing in 5