[API Proposal]: Provide API for freeing Variant memory #79402
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-System.Runtime.InteropServices
Milestone
Background and motivation
It is currently quite hard to reclaim memory allocated internally by the
Marshal.GetNativeVariantForObject
method. Let's take the following code as an example:We are leaking memory because
GetNativeVariantForObject
assigns the string to theAsBstr
property of the internalVariant
type. TheAsBstr
property internally allocates memory usingMarshal.StringToBSTR
. One solution could be to provide theMarshal.FreeNativeVariant
method, which would reclaim the internal memory.API Proposal
API Usage
Alternative Designs
Another approach that comes to my mind is to modify the
BStrWrapper
class so it will be responsible for allocating and freeing memory for the wrapped BSTR string. Then we would need to instruct the users to use the wrapper when callingGetNativeVariantForObject
as using raw string leads to a leak.GetNativeVariantForObject
already acceptsBStrWrapper
, so we won't need to modify the public API.Risks
No response
The text was updated successfully, but these errors were encountered: