description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Learn more about: Allocating and Releasing Memory for a BSTR |
Allocating and Releasing Memory for a BSTR |
11/04/2016 |
|
|
98041e29-3442-4a02-b425-7a4a13e9cc84 |
When you create BSTR
s and pass them between COM objects, you must take care in treating the memory they use in order to avoid memory leaks. When a BSTR
stays within an interface, you must free its memory when you are done with it. However, when a BSTR
passes out of an interface, the receiving object takes responsibility for its memory management.
In general, the rules for allocating and releasing memory allocated for BSTR
s are as follows:
-
When you call into a function that expects a
BSTR
argument, you must allocate the memory for theBSTR
before the call and release it afterwards. For example:[!code-cppNVC_ATLMFC_Utilities#192] [!code-cppNVC_ATLMFC_Utilities#193]
-
When you call into a function that returns a
BSTR
, you must free the string yourself. For example:[!code-cppNVC_ATLMFC_Utilities#194] [!code-cppNVC_ATLMFC_Utilities#195]
-
When you implement a function that returns a
BSTR
, allocate the string but do not free it. The receiving function releases the memory. For example:[!code-cppNVC_ATLMFC_Utilities#196]
Strings (ATL/MFC)
CStringT::AllocSysString
SysAllocString
SysFreeString