How to create a custom shared_ptr?

  c++, destructor, shared-ptr, smart-pointers

Say I have a library SXY which gives me a picture from a file:

Picture * pic;


And I share it over multiple functions. But I want to call picture_close(pic) to dellocate it only when I am done and all these functions went out of a scope. shared_ptr already does that but if I do:

shared_ptr<Picture> pic=get_picture("file")

It won’t won’t compile first because the function get_picture returns a pointer to a Picture and not a shared_ptr and when it goes out of scope it won’t call picture_close(pic) which is the proper way to deallocate it. Is there by any chance a custom form of a shared pointer that calls a sort of destructor only when all references to this picture went out of scope?

If I do a object and call the picture_close(pic) in the destructor of it’s class it will call the destructor and deallocate the picture whenever the object is copied which is what I am having.

Source: Windows Questions C++