SysFreeString memory leak?

The test program attached results in apparent memory leaks. I believe they are related to SysAllocString/SysFreeString. The memory leaks are "gone" when the program exits, but they are a problem in a constantly running application. I have tested the program on the WM5 emulator and on a WM5 Pocket PC device.

// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Test.h"
#include <windows.h>
#include <commctrl.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// The one and only application object

CWinApp theApp;

using namespace std;

//////////////////////////////////////////////////////////////////////////

class testclass
{
#define MAX_ELEMENTS 5
COleVariant m_DataArray[MAX_ELEMENTS];

public:
void test ()
{

for (int i = 0; i < MAX_ELEMENTS; i++)
{
m_DataArrayIdea.Clear();
}
CString szData = L"test test test test";

for (int i = 0; i < MAX_ELEMENTS; i++)
{
m_DataArrayIdea = szData;
}
}
};
//////////////////////////////////////////////////////////////////////////


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
}
//////////////////////////////////////////////////////////////////////////
{
testclass* pTest;

pTest = new testclass;
for (int i=0; i<1; i++)
{
pTest->test();
}
delete pTest;
}

BSTR bstr = ::SysAllocString(L"string string string string");
::SysFreeString(bstr);
//////////////////////////////////////////////////////////////////////////

return nRetCode;
}

What can I do to correct this problem

Thanks,

Frank



Answer this question

SysFreeString memory leak?

  • Julianin

    Could the problem be the BSTR cache as described in the blog below

    http://blogs.msdn.com/netcfteam/http://blogs.msdn.com/netcfteam/

    .NET Compact Framework Team
    "4. There appears to be a memory leak when using BSTR marshaling, due to a native cache in the Windows CE OS.
    The memory used for BSTR allocations is cached, so a BSTR is not always freed completely upon a call to SysFreeString. There is currently no way to turn this cache off under Windows CE, and given the low memory conditions of embedded devices this may present itself as an unacceptable memory leak. To work around this issue, consider using CoTaskMemFree instead of SysFreeString, which prevents any free space in the cache from accumulating.

    The runtime will use SysFreeString under two conditions:

    * During the marshaling process when you specify MarshalAs(UnmanagedType.BSTR) for a parameter, array element, or structure field. You can prevent this from happening by using IntPtr marshaling, and manually converting strings to BSTRs before calling the native function using InteropServices.Marshal.StringToBSTR.
    * The Marshal.FreeBSTR managed API maps directly to SysFreeString.

    The native COM component being used will also need to be screened for SysFreeString use.
    You can see some more information about the BSTR cache here:
    http://msdn.microsoft.com/library/default.asp url=/library/en-us/automat/htm/chap7_2xgz.asp

    "

    The leaked memory looks like a BSTR with a "rtsb" (bstr reversed) prefix. Is this a characteristic of the BSTR cache

    Frank


  • Cliff Stanford

    I was going to reply to your original message to tell you if it was on the desktop I'd bet on it being caused by BSTR caching, but I didn't know if CE did that. With the information above, I'd say it's almost certain that this is what you're seeing.

  • DaedMajaz

    Assuming this is indeed BSTR caching, what are my options for avoiding this problem (Similar to oanocache for the desktop)

    Thanks,

    Frank


  • SysFreeString memory leak?