The "Lumi" project goal is to provide tools for writing safe and efficient code.
This lumi-safe-ptr library provides C++ smart pointers wrappers that makes
them even more safe to use.
While C++ smart pointers help a lot in writing memory safe code - they still
have some pitfalls and problems that users can mistakenly fall into. These are
the issues that lumi-safe-ptr library solves:
- The infamous
shared_ptrcircular reference issue - More planned in the future...
This is a header-only library with a single header file located in
include/lumi-safe-ptr.hpp.
This file can be simply downloaded and included by
#include "lumi-safe-ptr.hpp". All macros start with LUMI_ prefix and all
other symbols are inside lumi:: namespace.
This is the most known shared_ptr issue that is now avoidable 😃.
It can be solved using macros LUMI_CYCLE_CHECK and LUMI_STRONG_PTR that will
raise a compilation error when a circular reference is detected. Their usage is
simple:
- Every
shared_ptrmember in a type should be declared usingLUMI_STRONG_PTR(type) name;(instead ofstd::shared_ptr<type> name;). - Before Every type that uses
LUMI_STRONG_PTRadd (in the global scope)LUMI_CYCLE_CHECK(type, pointed_types...)
For example:
#include "lumi-safe-ptr.hpp"
LUMI_CYCLE_CHECK(module::A, module::B, module::C)
namespace module {
class A {
LUMI_STRONG_PTR(B) b;
LUMI_STRONG_PTR(C) c;
};
}More Issues that may be solved next as long as they can be technically done using C++ syntax:
- Access moved
unique_ptr - Using unchecked
weak_ptr - Deleting the raw pointers
- Thread safety
- Using non-heap pointers
- Not using
make_shared - Safe arrays of pointers
- Derived to base class pointer
- Garbage collecting pointer