1

I am using CComBSTR in below scenario,

void MyDlg::OnTimer()
{

      ......

      CComBSTR statusString1 = ::SysAllocString(_T("Test"));

      ....

}

The timer will get executed with in each 5 seconds interval.

In the above case the meory is getting increased for each 5 seconds. As per my understanding the CComBSTR does the memory clean up when it goes out of scope. So the allocated memory has to be freed whenever the timer finishes. But its not.

I need to understand when the memory get freed when the CCOMBSTR is used.

srajeshnkl
  • 883
  • 3
  • 16
  • 49
  • How do you measure memory increase? – Roman R. Aug 16 '12 at 07:56
  • Case 1: I use Windows Task manager and got tne memory usage in "Memory used"; In this case If i create dynamic memory and delete, its getting increased and decreased. But in case of CCOMBSTR, its not. Case 2: I use GlowCode and see the the Current bytes. – srajeshnkl Aug 16 '12 at 08:57
  • Task manager does now show you reliable indication. Use Performance Monitor or Process Explorer instead, see http://stackoverflow.com/questions/1984186/what-is-private-bytes-virtual-bytes-working-set for details. – Roman R. Aug 16 '12 at 09:02

1 Answers1

4

Your use of CComBSTR is wrong. CComBSTR is making a copy of the allocated string NOT taking ownership of it. You can just initialize your CComBSTR like this:

CComBSTR statusString1( L"Test" );

If you want to take ownership of a previously allocated string do this:

BSTR bstrAlloc = ::SysAllocString(_T("Test"));
... Your Code ...
CComBSTR status;
status.Attach( bstrAlloc );

Then when the CComBSTR goes out of scope it will destroy the allocated string.

More info: I recommend having a look at the implementation of CComBSTR in atlcomcli.h (usually located in the C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\include folder). It's not complicated.

snowdude
  • 3,854
  • 1
  • 18
  • 27