

You as the caller don’t know or care whether the function copied or transferred a reference to the stream into your recipient pointer all that you care about is that when the function returns, your recipient pointer received a reference to the thing.īonus chatter: C++ shared_ptr and unique_ptr have similar patterns and pitfalls. This pattern is used by most of COM: For example, CreateStreamOnHGlobal takes a recipient as its final parameter, and it puts a reference to the newly-created stream in that recipient. The “recipient” pattern produces a T**, and it’s up to the donor to decide whether to transfer or copy ownership to it. The purpose of the above tables is to help you move between rows: The donor and recipient should both be attach/detach (transfer) semantics, or they should both be get/copy (share) semantics.īut wait, there’s another option, which I will call the “recipient” pattern. For example, you can just use sp1 = sp2 to copy one smart pointer to another smart pointer of the same type, or sp1 = std::move(sp2) to transfer ownership. Of course, if you are remaining within one row of the table, then you can usually avoid having to operate through ABI pointers. Note 1: IPTR’s Detach() method does not return the raw pointer.Īnd then the get/copy (share) operations: Library For example, some of the methods require that the smart pointer be non-null.įirst, the attach/detach (transfer) operations: Library Note that this table is just an overview consult the corresponding documentation for further information.
#Copying near me windows#
Here are some tables showing various Windows smart pointer libraries and how they express the two pairs of operations.

If the donor offers with “get” but the recipient attaches, then you have an over-release. If you mess up, then you end up with reference count bugs: If the donor detaches but the recipient copies, then you have a reference count leak. If you want to share ownership, then the donor should use get semantics and the recipient should use copy semantics. It is important that when you move raw pointers between smart pointers, that you match up the two semantics: If you want to transfer ownership, then the donor should use detach semantics and the recipient should use attach semantics.
