crimson_cell::intrusive_ptr is a lightweight, header-only intrusive smart pointer implementation for modern C++ inspired by boost implementation.
It provides reference-counted ownership without requiring a separate control block, keeping the smart pointer itself to a single pointer-sized member. Reference counting is stored directly in the managed object, making the library suitable for applications where minimal overhead and predictable memory usage are important.
The library is designed to be simple, modern, and easy to integrate into existing projects.
intrusive_ptr is intended for class objects that implement their own reference counting. The reference count is stored directly in the object, allowing the smart pointer to participate in the object's existing lifetime-management mechanism.
This is particularly useful when working with APIs or object models that already expose AddRef/Release-style ownership.
Using std::shared_ptr in such systems can require maintaining two separate ownership mechanisms: the object's internal reference count and shared_ptr's external control block. Keeping both mechanisms synchronized is tedious and fragile, and can lead to incorrect lifetime management.
With intrusive_ptr:
- The object owns its reference count —
AddRef/Releaseoperate directly on the managed object. - No duplicated ownership state — There is no separate
shared_ptrcontrol block that must be kept in sync. - Minimal footprint — The
intrusive_ptritself contains only a single pointer. - No additional allocation — No separate control block is required.
- Works with existing reference-counted objects — Particularly useful for COM-style and other intrusive reference-counted APIs.
- Header-only — No library to build or link against.
The trade-off is that the managed type must explicitly participate in the reference-counting mechanism.
intrusive_ptr is designed for objects that implement their own reference-counting mechanism. If your object does not already use intrusive reference counting, std::shared_ptr is usually the more convenient choice.
You may want to use std::shared_ptr instead when:
- Your object does not implement its own reference counting.
- You need thread-safe reference-count manipulation out of the box.
- You want to keep lifetime management completely separate from the managed object's implementation.
- You need to manage arbitrary types without requiring them to participate in the ownership mechanism.
In short, intrusive_ptr is best suited for objects that already own their reference-counting mechanism. If you simply need shared ownership of an otherwise ordinary type, std::shared_ptr is generally the simpler choice.
Download the header from the Releases page and include crimson_cell/intrusive_ptr.hpp.
Alternatively you can also add the repository as a git submodule and include it in your CMake project via add_subdirectory function.
The full API reference and documentation are available on the project documentation site.
Install the required dependencies:
Windows
winget install DimitriVanHeesch.DoxygenUbuntu / Debian
sudo apt install doxygenpip install -r docs/requirements.txtAfter installing the dependencies, run the appropriate build script.
Windows
docs/build.batUbuntu / Debian
docs/build.shTo use intrusive_ptr, define intrusive_ptr_add_ref and intrusive_ptr_release to connect it to your object's AddRef() and Release() methods.
#include <crimson_cell/intrusive_ptr.hpp>
#include <iostream>
class MyObject {
public:
void Hello() const {
std::cout << "Hello from MyObject!\n";
}
void AddRef() noexcept {
++ref_count_;
}
void Release() noexcept {
if (--ref_count_ == 0) {
delete this;
}
}
private:
int ref_count_ = 0;
};
void intrusive_ptr_add_ref(MyObject* object) noexcept {
object->AddRef();
}
void intrusive_ptr_release(MyObject* object) noexcept {
object->Release();
}
int main() {
// The regular constructor calls AddRef().
crimson_cell::intrusive_ptr<MyObject> first{new MyObject};
first->Hello();
{
// Copying the pointer acquires another reference.
auto second = first;
// Both pointers share the same object.
second->Hello();
}
// 'second' was destroyed, so its reference was released.
// 'first' still owns a reference to the object.
// Simulate an external API returning an object with an
// already-acquired reference.
MyObject* raw = new MyObject;
raw->AddRef();
{
// Adopt the existing reference without calling AddRef().
crimson_cell::intrusive_ptr<MyObject> adopted{raw, crimson_cell::adopt_ref};
adopted->Hello();
// When 'adopted' is destroyed, Release() is called.
// No additional reference was acquired during adoption.
}
// The adopted object has now been destroyed because its
// reference count reached zero.
return 0;
}intrusive_ptr<T>{ptr}callsintrusive_ptr_add_ref(ptr)to acquire a reference.intrusive_ptr<T>{ptr, crimson_cell::adopt_ref}adopts an existing reference without incrementing the reference count. The adopted pointer still callsintrusive_ptr_release(ptr)when its ownership ends.
Important: Only use adopt_ref when a reference has already been acquired for the pointer being adopted. Otherwise, the pointer may release a reference it does not own, potentially destroying the object prematurely.
The project includes a comprehensive unit test suite built with GoogleTest.
Those tests cover the core functionality of intrusive_ptr, including:
- Reference counting and ownership semantics
- Copy and move operations
- Pointer conversions
- Reset and detach operations
- Pointer comparisons and casts
- Reference-counted object lifetime
- Edge cases and regression tests
The project uses GitHub Actions to automate development and release workflows.
The verify.yml workflow runs automatically on every push and pull request. It checks code formatting, builds the library on Linux and Windows, and runs the unit test suite on those platforms.
The release.yml and docs.yml workflows automate the process of publishing new versions, helping keep releases consistent and reproducible.
This project is licensed under the MIT License. See LICENSE for details.