2 Sep 2004 09:31
Re: Cstring question Again
<tarun.kanal <at> ...> writes:
>
> Hi All
>
> I've come across a piece of code.
>
> This is defined in AtlBase.h line number 3977
>
> BSTR Detach()
> {
> BSTR s = m_str;
> m_str = NULL;
> return s;
> }
>
> Now in this method m_str is being made NULL without being released.
> Is this correct? Its giving me a memory leak out here.
>
Yes, this is correct. The alternative could be:
BSTR Detach()
{
BSTR garbage = m_str;
::SysFreeString(m_str); // This line trashes 'garbage'
m_str = NULL;
return garbage;
}
See what I mean? This function HAS to return a VALID BSTR.
> My Code is as follows
>
> CComBSTR bstrTemp = T2W(szData);
> *bstrBuffer = bstrTemp.Detach(); //this is giving a memory leak
>
> Any pointers how to tackle this or is my approach wrong??
>
Option 1:
CComBSTR bstrTemp = T2W(szData);
*bstrBuffer = bstrTemp.Detach(); //this is giving a memory leak
// After returning from wherever the former happened...
DoStuffWith(bstrBuffer);
DoMoreStuffWith(bstrBuffer);
// And, finally.
::SysFreeString(*bstrBuffer);
Option 2:
{
CComBSTR bstrTemp = T2W(szData);
DoStuffWith(bstrTemp);
DoMoreStuffWith(bstrTemp);
// bstrTemp's destructor cleans up very nicely.
}
See also:
http://www.devguy.com/fp/Tips/COM/bstr.htm
Best wishes,
Pablo.
> Regards
> Tarun
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
------------------------ Yahoo! Groups Sponsor --------------------~-->
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/saFolB/TM
--------------------------------------------------------------------~->
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/wtl/
<*> To unsubscribe from this group, send an email to:
wtl-unsubscribe@...
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
RSS Feed