Using deallocator & allocator in shared_ptr

I use several library functions that return a pointer created using either malloc or new. So, I have my own client deactivator based on what type of distribution was used.

eg

shared_ptr<int> ptr1(LibFunctA(), &MallocDeleter); //LibFunctA returns pointer created using malloc shared_ptr<int> ptr2(LibFunctB(), &newDeleter); //LibFunctB returns pointer created using new 

Now, I understand this is a very naive use of the deallocator above, but what other scripts are used for this?

In addition, how can I use a customer distributor? I tried to assign a custom dispenser as shown below, but now how do I call it? Where does this feature help?

 shared_ptr<int> ptr3(nullptr_t, &CustomDeleter, &CustomAllocator); //assume both functs are defined somewhere. 
+4
source share
3 answers

I do not see anything β€œnaive” about using deletions this way. After all, this is the main purpose of this function; to destroy pointer objects that are not allocated using standard C ++ methods.

Highlighters are designed when you need to control how the shared_ptr control block is allocated and deleted. For example, you may have a memory pool from which you want to get these things, or if you are in a situation with limited memory when allocating memory through new simply unacceptable. And since the type of control unit is before shared_ptr , there is no other way to control how it is distributed, except with some sort of distributor.

+6
source

Custom removers for shared_ptr very useful for transferring some (usually) C resource, which you need to call the release function later on. For example, you can do something like:

 shared_ptr<void> file(::CreateFileW(...), ::CloseHandle); 

Examples like this exist in C libraries. This eliminates the need to manually release the resource later and take care of possible exceptions and other nasty things.

+3
source

I think the custom allocator will be used to allocate space for the shared count object, which stores a copy of the deleocator and reference count.

As for which custom divider can be used to ...

One use was mentioned: make shared_ptr compatible with objects that must be deleted using some special function (for example, FILE , which is deleted by fclose ), without having to bind it to a helper class that takes care of proper deletion.

Another use for custom deletion is pools. The pool can pass shared_ptr<T> , which were initialized by a "special" deleter, which does not actually delete anything, but instead returns the object to the pool.

And one more thing: a debugger is already needed to implement some shared_ptr functions. For instance. the deleted type is always fixed at creation time and does not depend on the initialization type shared_ptr .

Vou can create a shared_ptr<Base> , actually initializing it with Derived . shared_ptr ensures that when an object is deleted, it will be deleted as Derived , even if Base does not have a virtual dtor. To make this possible, shared_ptr should already store some information about how the object should be deleted. Thus, allowing the user to specify a fully customizable deaeter does not cost anything (in terms of performance at runtime) and does not require special additional code.

There are probably dozens of other scenarios in which you can efficiently use a custom deleter, this is what I came up with so far.

+1
source

Source: https://habr.com/ru/post/1398138/


All Articles