0

I have a code like-

void CSomeClass::Remove()
{
  BSTR tempStr = NULL;

  while(!m_list.IsEmpty()) //m_list is a CSomeClass member of type CList<BSTR, BSTR>
  {
    tempStr = m_list.RemoveHead(); //application crash here!!

    if(NULL==tempStr)
       continue;
  }

  SysFreeString(tempStr);

}

And I am not sure why the application got crash. Is it possible to initialize a BSTR to another BSTR using assignment operator? Can anyone help me in finding out why the application is crashing?

BaluS
  • 55
  • 6
  • 2
    `BSTR` is merely a typedef for `wchar_t*`. Yes of course you can assign a pointer to a variable. Your problem lies elsewhere - most likely, heap corruption (somewhere, possibly in a completely unrelated piece of code; heap corruption is nasty this way). – Igor Tandetnik Jun 30 '16 at 13:20

2 Answers2

0

Yes. BSTR can be assigned to another BSTR variable. BSTR is actually the starting address of the actual data.

The problem here is with the RemoveHead() function and not the assignment. Please see the complete stack trace or just attach a debugger to your program to debug the issue further.

-1

Put the SysFreeString inside the loop

void CSomeClass::Remove()
{
  BSTR tempStr = NULL;

  while(!m_list.IsEmpty()) //m_list is a CSomeClass member of type CList<BSTR, BSTR>
  {
    tempStr = m_list.RemoveHead(); //application crash here!!

    if(NULL==tempStr)
       continue;
    SysFreeString(tempStr);
  }


}
Joseph Willcoxson
  • 5,853
  • 1
  • 15
  • 29