Suppose I have implemented a scoped_ptr
:
template <typename T> class scoped_ptr {
public:
scoped_ptr() = delete;
explicit scoped_ptr(T *ptr) : _ptr(ptr){};
~scoped_ptr() {
delete _ptr;
_ptr = nullptr;
};
scoped_ptr(const scoped_ptr &p) = delete;
scoped_ptr &operator=(const scoped_ptr &p) = delete;
T *operator->() const { return _ptr; }
T &operator*() const { return *_ptr; }
T *get() const { return _ptr; }
void reset(T *p = nullptr) {
delete _ptr;
_ptr = p;
}
private:
T *_ptr;
};
I want to test that the memory is actually freed after the pointer's lifetime is over, but I cannot verify this by dereferencing that raw pointer _ptr
because it is expected that the memory it pointed to should have already been released. Then how do I test it?