Skip to content

AMDmi3/dangling_ptr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dangling::ptr

This is simple header-only library implementing class of pointers which automatically track lifetime of their pointees - when pointee is destroyed, pointer automatically resets to nullptr, so there is a way to know that object it references no longer exists and accessing freed memory is impossible.

Synopsis

#include <dangling_ptr.hh>

class MyObject : public dangling::target<MyObject> {
public:
    bool MyMethod() {
        return true;
    }
};

...

{
    dangling::ptr<MyObject> ptr;

    {
        MyObject obj;
        ptr = &obj;

        assert(ptr);
        assert(ptr.get() == &obj);
        assert(ptr->MyMethod());
    }

    // object is no more, but pointer is aware of it
    assert(!ptr);
    assert(ptr.get() == nullptr)
	try {
		ptr->MyMethod();
    } catch (dangling::bad_access& e) {
        std::cerr << "attempt to access destroyed object" << std::endl;
    }
}

Author

License

2-clause BSD, see COPYING.

About

Non-owning smart pointer which is aware of pointee destruction

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published