You can use unique_ptr to remove memory, but you need to provide your own Deleter class, because memory is allocated using malloc , not new .
Even better, change the distribution code to use new and just use unique_ptr . If you take this road, you can simply return the unique_ptr instead of a pointer to memory.
Assuming you need to provide your own custom debugger, here is one way you can do this:
template <typename T> class MallocDeleter { public: void operator() (T* obj) const { LegacyDeleter (*obj); } }; typedef std::unique_ptr <LegacyObj, MallocDeleter <LegacyObj>> unique_legacy_ptr;
Perhaps you can also provide a function of type make_unique_legacy that allocates with a call to LegacyAllocator , rather than initializing unique_ptr .
source share