none
CoTaskMemAlloc/CoTaskMemFree - do I need to explicitly free on the Managed code side? RRS feed

  • Question

  • Hi, In the following code...
    
    
    Unmanaged C++
    ===========
    
    unsigned long CPLUSPLUS_EXPORT Echo(unsigned long allocSize, LPTSTR* pchXmlResultOut);
    unsigned long Echo(unsigned long allocSize, LPTSTR* pchXmlResultOut)
    {
    
    	CoTaskMemFree(*pchXmlResultOut);
    	*pchXmlResultOut = (LPTSTR)CoTaskMemAlloc(allocSize+1);
    	_sntprintf(*pchXmlResultOut, (allocSize+1), "ABCEFJ%ld", allocSize);
    	return 134;
    }
    
    
    Managed Code:
    ==========
    
     const string PmCoreDtDll = @"cplusplus.dll";
     [DllImport(PmCoreDtDll)]
     public static extern uint Echo(int size, ref string pchXmlResultOut);
    
    
    
    // Call C++ unmanaged function
    string dtReply = string.Empty;
    Echo("AAA", bufSize, ref dtReply);
    
    
    

    In the above code, do I need to do anything specific on the Managed Code side to release the memory allocated on the Unmanaged code side?
    From what I read at http://msdn.microsoft.com/en-us/magazine/cc164193.aspx, I don't have to do anything.
    But, I have a test program that calls into that C++ code with 800000000 as the size of the string to be returned, in a loop.
    When I notice the Private Bytes of the process, it seems to be leaking memory.  Not exactly sure if I am missing anything.

    Any insight would be greatly appreciated.

    Regards


    - Athadu
    • Edited by Athadu123 Monday, July 19, 2010 4:10 PM Removed blank lines and formatted code properly
    Monday, July 19, 2010 3:46 PM

Answers

  • Hi,

      Just based on your code snippet, if you don't free the memory, nobody will. So I think you need to call the CoTaskMemFree from the managed code.

      Below is some quotation from the article you mentioned.

      "Since this involves memory allocation and de-allocation, the biggest problem is what function to use. There are many to choose from: HeapAlloc/HeapFree, malloc/free, new/delete, and so on. However, since the CLR uses CoTaskMemAlloc/CoTaskMemFree in the non-BSTR case and SysStringAlloc/SysStringAllocByteLen/SysStringFree in the BSTR case, you'll have to use those functions. Otherwise, it is likely that you'll get a memory leak or a crash in certain versions of Windows®. "


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Marked as answer by SamAgain Tuesday, July 27, 2010 10:10 AM
    Tuesday, July 20, 2010 6:58 AM

All replies

  • Hi,

      Just based on your code snippet, if you don't free the memory, nobody will. So I think you need to call the CoTaskMemFree from the managed code.

      Below is some quotation from the article you mentioned.

      "Since this involves memory allocation and de-allocation, the biggest problem is what function to use. There are many to choose from: HeapAlloc/HeapFree, malloc/free, new/delete, and so on. However, since the CLR uses CoTaskMemAlloc/CoTaskMemFree in the non-BSTR case and SysStringAlloc/SysStringAllocByteLen/SysStringFree in the BSTR case, you'll have to use those functions. Otherwise, it is likely that you'll get a memory leak or a crash in certain versions of Windows®. "


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Marked as answer by SamAgain Tuesday, July 27, 2010 10:10 AM
    Tuesday, July 20, 2010 6:58 AM
  • Hi,

      I am writing to check the status of the thread. How is the problem going on?


    Please mark the right answer at right time.
    Thanks,
    Sam
    Monday, July 26, 2010 11:11 AM
  • We temporarily mark a reply, please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Please mark the right answer at right time.
    Thanks,
    Sam
    Tuesday, July 27, 2010 10:10 AM