Initializing a BSTR to another BSTR using assignment operator

256 Views Asked by At

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?

2

There are 2 best solutions below

0
Joseph Willcoxson On

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);
  }


}
0
Deepan Negi On

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.