I have read that the following code causes memory leak. But did not understand why.
CComBSTR str;
pFoo->get_Bar(&str);
pFoo->get_Baf(&str);
How does it cause a leak when we are not allocating anything?
I have read that the following code causes memory leak. But did not understand why.
CComBSTR str;
pFoo->get_Bar(&str);
pFoo->get_Baf(&str);
How does it cause a leak when we are not allocating anything?
It leaks because get_Bar()
and get_Baf()
don t know that you re using a CComBSTR.
When you take the address of a CComBSTR what you re actually passing to the underlying object is a pointer to the CComBSTR s BSTR member.
Breaking down the sequence:
CComBSTR str;
This initializes the internal BSTR to NULL.
pFoo->get_Bar(&str);
get_Bar()
sees a BSTR* and fills it with actual data. Like this:
HRESULT get_Bar(BSTR* arg) { *arg = SysAllocString(L"My String"); }
Now the internal BSTR of str
is a real BSTR. When CComBSTR goes out of scope it will delete the str
member.
Now if you call get_Baf()
on &str the problem is that the CComBSTR doesn t know that you are changing the string. So you call get_Baf()
like this:
HRESULT get_Baf(BSTR* arg) { *arg = SysAllocString(L"My String"); }
Now get_Baf()
has overwritten the original value of str
s internal BSTR without anyone freeing the data that was allocated by get_Bar()
.
Ta da - Memory leak.
This Microsoft page is probably the where you read about it:
http://msdn.microsoft.com/en-us/library/bdyd6xz6.aspx
Memory Leak Issues
Passing the address of an initialized CComBSTR to a function as an [out] parameter causes a memory leak.
The CComBSTR object is allocating memory internally. Evidently there are cases where it doesn t release it.
I am trying to add a button to one of the existing tool bars in any windows explorer instance. After much research i figured out that BHO (browser helper objects) are the best way to hook to ...
I m finding leaked heap blocks by using the following command in WinDbg !heap –l With each leaked heap block I get, I m running to following to get the stack trace. !heap -p -a leakedheapblock The ...
I m new to COM. What exactly is the advantage of replacing: L"String" with CComBSTR(L"String") I can see a changelist in the COM part of my .NET application where all strings are replaced in this ...
ATL provides a bunch of macros for creating so-called COM maps - chains of rules of how the QueryInterface() call behaves on a given object. The map begins with BEGIN_COM_MAP and ends with END_COM_MAP....
I am trying to develop an IThumbnailProvider for use in Windows 7. Since this particular thumbnail would also be dependant on some other files in the same directory, I need to use something other than ...
What s the best way to get a chunk of memory (i.e. void*) from a COM server to C#? We have been using an IStream (using CreateStreamOnHGlobal) and passing that back, which worked. However when we ...
I m trying to roll my own implementation of IShellBrowser because I need to have a more full-featured File Open and Save As dialog than Windows allows that is compatible with XP (and ideally with ...
folks can you provide me the tutorial link or .pdf for learning basic COM?. i do google it.. still i recommend answers of stackoverflow so please pass me.. Thanks