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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:26:53+00:00 2026-05-12T09:26:53+00:00

I have an out-of-process COM server written in C++, which is called by some

  • 0

I have an out-of-process COM server written in C++, which is called by some C# client code. A method on one of the server’s interfaces returns a large BSTR to the client, and I suspect that this is causing a memory leak. The code works, but I am looking for help with marshalling-out BSTRs.

Simplifying a bit, the IDL for the server method is

HRESULT ProcessRequest([in] BSTR request, [out] BSTR* pResponse);

and the implementation looks like:

HRESULT MyClass::ProcessRequest(BSTR request, BSTR* pResponse)
{
    USES_CONVERSION;
    char* pszRequest = OLE2A(request);
    char* pszResponse = BuildResponse(pszRequest);
    delete pszRequest;
    *pResponse = A2BSTR(pszResponse);
    delete pszResponse;
    return S_OK;
}

A2BSTR internally allocates the BSTR using SysAllocStringLen().

In the C# client I simply do the following:

string request = "something";
string response = "";
myserver.ProcessRequest(request, out response);
DoSomething(response);

This works, in that request strings get sent to the COM server and correct response strings are returned to the C# client. But every round trip to the server leaks memory in the server process. The crt leak detection support is showing no significant leaks on the crt heap, so I’m suspecting the leak was allocated with IMalloc.

Am I doing anything wrong here? I have found vague comments that ‘all out parameters must be allocated with CoTaskMemAlloc, otherwise the interop marshaller won’t free them’ but no details.

Andy

  • 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-12T09:26:54+00:00Added an answer on May 12, 2026 at 9:26 am

    anelson has covered this pretty well, but I wanted to add a couple of points;

    • CoTaskMemAlloc is not the only COM-friendly allocator — BSTRs are recognized by the default marshaller, and will be freed/re-allocated using SysAllocString & friends.

    • Avoiding USES_CONVERSION (due to stack overflow risks — see anelson’s answer), your full code should be something like this [1]

    (note that A2BSTR is safe to use, as it calls SysAllocString after conversion, and doesn’t use dynamic stack allocation. Also, use array-delete (delete[]) as BuildResponse likely allocates an array of chars)

    • The BSTR allocator has a cache that can make it appear as though there is a memory leak. See http://support.microsoft.com/kb/139071 for some details, or Google for OANOCACHE. You could try disabling the cache and see if the ‘leak’ goes away.

    [1]

    HRESULT MyClass::ProcessRequest(BSTR request, BSTR* pResponse)
    {
        char* pszResponse = BuildResponse(CW2A(request));
        *pResponse = A2BSTR(pszResponse);
        delete[] pszResponse;
        return S_OK;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Out-of-process COM server executable which was developed using Visual Studio 6.0
When you have an out of process COM Server and you call a function
When you have a piece of software that runs an out of process COM
I need to create an out-of-process COM server (.exe) in C# that will be
It is about Window COM component. Server.exe: an 32bit out-of-process COM server CLSID_Application: The
I have read about storing session state out-of-process but still in memory by configuring
I'm trying to figure out how XSLT process namespace prefixes and have following example:
I have a daily batch process that involves selecting out a large number of
I have a console app that performs a lengthy process. I am printing out
I have checked out code from a svn repository and started making changes. But

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.