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 7925717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:25:28+00:00 2026-06-03T18:25:28+00:00

If I have managed COM interface called from unmanaged code, am I responsible for

  • 0

If I have managed COM interface called from unmanaged code, am I responsible for freeing up the memory after use or will it be handled by garbage collection?

 public void WriteOutFile([In] [MarshalAs(UnmanagedType.BStr)] String data)
 {
      File.WriteAllText(fileName, data);
      //do I need the line below??
      Marshal.FreeBSTR(data);
 }

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-06-03T18:25:30+00:00Added an answer on June 3, 2026 at 6:25 pm

    You should not free the string because the caller can potentially reuse the data passed and if you free it is possible to have a fault. The reason is that FreeBSTR does not use any reference count mechanism and simply call SysFreeString, that by the way assumes that the string is allocated with one of the function Sys(Re)Alloc..., circumstance you are not aware of in the managed code.
    The example shown here is interesting, imagin the unmanaged code calling you is this one ( from the link before ):

    // shows using the Win32 function 
    // to allocate memory for the string: 
    BSTR bstrStatus = ::SysAllocString(L"Some text");
    if (bstrStatus != NULL)
    {
       pBrowser->put_StatusText(bstrStatus);
       // Free the string:
       ::SysFreeString(bstrStatus);
    }
    

    and you have imlemented put_StatusText(...) in your managed code we are reproducing your situation. As you can see is the caller responsible on allocating/deallocating the parameter string, outside the callee.

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

Sidebar

Related Questions

We have a COM component who’s implementation and interface definition exist in managed code
I'm struggling with a scenario where I have a managed interface exposed through COM
Setup: I have a COM DLL that calls a method inside a managed C#
I have managed to crunch down a several line code to this For Each
I have a COM interface method definition in IDL as follows: [id(8)] HRESULT GetBinData([in,out,size_is(dataLen)]BYTE
I have existing managed and unmanaged software using an ActiveX component supplied by a
I'm trying to use the Microsoft Debug Interface Access SDK from C#. This is
When creating COM interface declarations in C# are there any rules you have to
recently I have been encountering problems with exposing events from .NET to COM. I
After much help from all my StackOverFlow brethren, I managed to create a C++

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.