Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6244139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:16:21+00:00 2026-05-24T12:16:21+00:00

I am writing writing an API in COM in C++, and also writing a

  • 0

I am writing writing an API in COM in C++, and also writing a program which consumes this API in C#. My question is about BSTR memory management semantics when passing BSTRs into COM functions. Say my IDL looks like:

HRESULT SomeFunction([in] BSTR input);

Currently this function is implemented like this:

HRESULT SomeFunction(BSTR input) {
    // Do stuff ..., then:
    SysFreeString(input);
}

When I call it from C# with something like SomeFunction(myString), will C# generate something like this (pseudocode):

myString = SysAllocString("string");
SomeFunction(myString);

Or rather like this:

myString = SysAllocString("string");
SomeFunction(myString);
SysFreeString(myString);

That is, does C# free the BSTR that it generates to marshal to the COM interface, or should I free it inside my function? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:16:23+00:00Added an answer on May 24, 2026 at 12:16 pm

    From Allocating and Releasing Memory for a BSTR:

    When you call into a function that expects a BSTR argument, you
    must allocate the memory for the BSTR before the call and
    release it afterwards. …

    So don’t free it if it is an input parameter. C# (and any other runtime that uses COM objects) must respect the COM convention for managing memory pass in and out of COM objects, and must therefore manage the memory for the string if it is an input parameter. Otherwise, how would a COM object know that it is being called from C# or some other language runtime?

    Additional google-fu turned up this: Marshaling between Managed and Unmanaged Code

    … Regarding ownership issues, the CLR follows COM-style
    conventions:

    • Memory passed as [in] is owned by the caller and should be both
      allocated by the caller and freed by the caller. The callee should
      not try to free or modify that memory.
    • Memory allocated by the callee and passed as [out] or returned
      is owned by the caller and should be freed by the caller.
    • The callee can free memory passed as [in, out] from the caller,
      allocate new memory for it, and overwrite the old pointer value,
      thereby passing it out. The new memory is owned by the caller. This
      requires two levels of indirection, such as char **.

    In the interop world, caller/callee becomes CLR/native code. The rules
    above imply that in the unpinned case, if when in native code you
    receive a pointer to a block of memory passed to you as [out] from
    the CLR, you need to free it. On the other hand, if the CLR receives
    a pointer that is passed as [out] from native code, the CLR needs to
    free it. Clearly, in the first case, native code needs to do the
    de-allocation and in the second case, managed code needs to do
    de-allocation.

    So the CLR follows the COM rules for memory ownership. QED.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently writing an API and an application which uses the API. I
I'm writing a program using C++ and the MySQL C API (version 5.1.31 ubuntu2).
I'm writing an API that converts actions performed by a non-technical user into Salesforce.com
I am writing a API over the top of a COM object and you
I am writing an API that updates a LOT of different fields in a
I am writing an API and have come across the following pattern: My API
We are writing an API for iphone developers and we don't know what the
I'm a C++ amateur. I'm writing some Win32 API code and there are handles
I'm writing C# code that uses the windows IP Helper API. One of the
I'm writing an XMPP client in Java using the Smack API. I registered a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.