Notifying actual owner after references count of Obj reaches zero

Viewed 61

The case is:

Imagine a manager that is responsible for creating and holding different kind of abstract resources (pseudocode)

class ResourcesManager { 
    fun Ref<ResourceA> createResourceA();
    fun Ref<ResourceB> createResourceB();
    
    private:
        container<ResourceA> aResources;
        container<ResourceB> bResources;
}

When the manager creates a new resource, it saves it inside and exposes it outside to caller through some sort of shared pointer (Ref<...>).

What I want to accomplish is to not delete the resource Obj when its count in Ref reaches 0. Instead, the actual owner (ResourcesManager) should be notified about it (it means that no one use that resource anymore). So ResourcesManager can decide what to do next, it can just delete the resource obj and remove from container, or it can just mark that this resource is free to use and later, when some one wants to create the same resource, it can be taken from the container instead of creating a new one.

What ways to do that?

shared_ptr calls delete itself after it reaches 0.

So apparently I need my own version of shared pointer that does not deletes itself, but which notifies the owner when no one use Obj anymore. Is that a right way?

1 Answers

So based on the comments I've decided to use custom deleter in std shared pointer.

But eventually i will create my own pool allocator and custom pointer so i can write more specific memory management for my application

Related