Memory Leak
-
I have the following piece of code in an ATL COM component that I have developed. What this piece of code does is create a COM instance of another object inside the same COM component. However, somewhere in this piece of code is a memory leak. I have tested the COM object that is being created here seperately and it does not have a leak so it must be somewhere in this creation/destruction code... could anyone please let me know if they see something glaringly wrong? Thanks. // Calculate the checksum HRESULT hr = NULL; IHash *IHash = NULL; // Now we will intilize COM hr = CoInitialize( 0 ); // Use the SUCCEEDED macro and see if we can get a pointer // to the interface if ( SUCCEEDED( hr ) ) { hr = CoCreateInstance( CLSID_Hash, NULL, CLSCTX_INPROC_SERVER, IID_IHash, (void**)&IHash ); if ( SUCCEEDED( hr ) ) { BSTR* szReturn = new BSTR; IHash->Digest( szTemp.m_str, szReturn ); IHash->Release(); szTemp.AppendBSTR( *szReturn ); delete szReturn; } }
-
I have the following piece of code in an ATL COM component that I have developed. What this piece of code does is create a COM instance of another object inside the same COM component. However, somewhere in this piece of code is a memory leak. I have tested the COM object that is being created here seperately and it does not have a leak so it must be somewhere in this creation/destruction code... could anyone please let me know if they see something glaringly wrong? Thanks. // Calculate the checksum HRESULT hr = NULL; IHash *IHash = NULL; // Now we will intilize COM hr = CoInitialize( 0 ); // Use the SUCCEEDED macro and see if we can get a pointer // to the interface if ( SUCCEEDED( hr ) ) { hr = CoCreateInstance( CLSID_Hash, NULL, CLSCTX_INPROC_SERVER, IID_IHash, (void**)&IHash ); if ( SUCCEEDED( hr ) ) { BSTR* szReturn = new BSTR; IHash->Digest( szTemp.m_str, szReturn ); IHash->Release(); szTemp.AppendBSTR( *szReturn ); delete szReturn; } }
I didn't think you could create BSTR's this way. I think you have to call SysAllocString(). Or could it be that you are not freeing your BSTR? Just a guess....
-
I have the following piece of code in an ATL COM component that I have developed. What this piece of code does is create a COM instance of another object inside the same COM component. However, somewhere in this piece of code is a memory leak. I have tested the COM object that is being created here seperately and it does not have a leak so it must be somewhere in this creation/destruction code... could anyone please let me know if they see something glaringly wrong? Thanks. // Calculate the checksum HRESULT hr = NULL; IHash *IHash = NULL; // Now we will intilize COM hr = CoInitialize( 0 ); // Use the SUCCEEDED macro and see if we can get a pointer // to the interface if ( SUCCEEDED( hr ) ) { hr = CoCreateInstance( CLSID_Hash, NULL, CLSCTX_INPROC_SERVER, IID_IHash, (void**)&IHash ); if ( SUCCEEDED( hr ) ) { BSTR* szReturn = new BSTR; IHash->Digest( szTemp.m_str, szReturn ); IHash->Release(); szTemp.AppendBSTR( *szReturn ); delete szReturn; } }
You're handling your BSTRs wrong. When a method returns a string through a BSTR*, you pass the address of a BSTR, not a BSTR* (if that makes sense). :)
BSTR bsReturn;
...
IHash->Digest ( szTemp.m_str, &bsReturn );
...
SysFreeString ( bsReturn );You can also use CComBSTR or _bstr_t which do the memory management for you. --Mike-- http://home.inreach.com/mdunn/ The Signature, back by popular demand: Buffy. Pajamas.
-
I have the following piece of code in an ATL COM component that I have developed. What this piece of code does is create a COM instance of another object inside the same COM component. However, somewhere in this piece of code is a memory leak. I have tested the COM object that is being created here seperately and it does not have a leak so it must be somewhere in this creation/destruction code... could anyone please let me know if they see something glaringly wrong? Thanks. // Calculate the checksum HRESULT hr = NULL; IHash *IHash = NULL; // Now we will intilize COM hr = CoInitialize( 0 ); // Use the SUCCEEDED macro and see if we can get a pointer // to the interface if ( SUCCEEDED( hr ) ) { hr = CoCreateInstance( CLSID_Hash, NULL, CLSCTX_INPROC_SERVER, IID_IHash, (void**)&IHash ); if ( SUCCEEDED( hr ) ) { BSTR* szReturn = new BSTR; IHash->Digest( szTemp.m_str, szReturn ); IHash->Release(); szTemp.AppendBSTR( *szReturn ); delete szReturn; } }
Generally BSTR creates memory leak.Hence in order to avoid it use either _bstr_t or CComBstr. And even instead of BSTR* use BSTR object and pass the address to the function. And free it through SysFreeString function. BSTR m_bstr; IHash->Digest(szTemp.m_str, &m_bstr); ... .. SysFreeString(m_bstr);;P ;P :p
-
I have the following piece of code in an ATL COM component that I have developed. What this piece of code does is create a COM instance of another object inside the same COM component. However, somewhere in this piece of code is a memory leak. I have tested the COM object that is being created here seperately and it does not have a leak so it must be somewhere in this creation/destruction code... could anyone please let me know if they see something glaringly wrong? Thanks. // Calculate the checksum HRESULT hr = NULL; IHash *IHash = NULL; // Now we will intilize COM hr = CoInitialize( 0 ); // Use the SUCCEEDED macro and see if we can get a pointer // to the interface if ( SUCCEEDED( hr ) ) { hr = CoCreateInstance( CLSID_Hash, NULL, CLSCTX_INPROC_SERVER, IID_IHash, (void**)&IHash ); if ( SUCCEEDED( hr ) ) { BSTR* szReturn = new BSTR; IHash->Digest( szTemp.m_str, szReturn ); IHash->Release(); szTemp.AppendBSTR( *szReturn ); delete szReturn; } }
Generally BSTR creates memory leak.Hence in order to avoid it use either _bstr_t or CComBstr. And even instead of BSTR* use BSTR object and pass the address to the function. And free it through SysFreeString function. BSTR m_bstr; IHash->Digest(szTemp.m_str, &m_bstr); ... .. SysFreeString(m_bstr);;P ;P :p