BUG: Memory Leak in ATL OLE DB Consumer Template Functions (271926)
The information in this article applies to:
- The Microsoft Active Template Library (ATL) 3.0, when used with:
- Microsoft Visual C++, 32-bit Enterprise Edition 6.0
- Microsoft Visual C++, 32-bit Professional Edition 6.0
- Microsoft Visual C++, 32-bit Learning Edition 6.0
This article was previously published under Q271926 SYMPTOMS
The following Active Template Library (ATL) OLE DB consumer template methods do not properly release allocated memory, and therefore cause a memory leak:
- OpenWithPromptFileName
- OpenFromFileName
- GetInitializationString
These are all methods of the CDataSource class, which is defined in the Atldbcli.h header file.
CAUSE
These CDataSource methods do not release the memory for a string that was allocated for them. These methods call OLE DB functions that allocate the memory for the string and populate it. The OLE DB functions call the CoTaskMemAlloc function to allocate memory for the string, and the ATL OLE DB consumer caller gets the string back as a result.
It is the caller's responsibility to free this memory. In this situation, the ATL code is the caller. It does not release the string after it is used, which causes the memory leak.
RESOLUTION
One workaround is to add code to free the allocated memory for the string at the the end of each of the methods listed above. The following code illustrates this workaround:
HRESULT GetInitializationString(BSTR* pInitializationString, bool bIncludePassword=false)
{
// If the datasource isn't open, we're not going to get an init string.
_ASSERTE(m_spInit != NULL);
CComPtr<IDataInitialize> spDataInit;
LPOLESTR szInitString;
HRESULT hr = CoCreateInstance(CLSID_MSDAINITIALIZE, NULL, CLSCTX_INPROC_SERVER,
IID_IDataInitialize, (void**)&spDataInit);
if (FAILED(hr))
return hr;
hr = spDataInit->GetInitializationString(m_spInit, bIncludePassword, &szInitString);
if (SUCCEEDED(hr))
*pInitializationString = ::SysAllocString(szInitString);
// Free the string here.
if(szInitString) CoTaskMemFree((void*)szInitString);
return hr;
}
HRESULT OpenWithPromptFileName(HWND hWnd = GetActiveWindow(),
DBPROMPTOPTIONS dwPromptOptions = DBPROMPTOPTIONS_NONE,
LPCOLESTR szInitialDirectory = NULL)
{
USES_CONVERSION;
CComPtr<IDBPromptInitialize> spDBInit;
HRESULT hr = CoCreateInstance(CLSID_DataLinks, NULL, CLSCTX_INPROC_SERVER,
IID_IDBPromptInitialize, (void**) &spDBInit);
if (FAILED(hr))
return hr;
CComPtr<IDBProperties> spIDBProperties;
LPOLESTR szSelected;
hr = spDBInit->PromptFileName(hWnd,
dwPromptOptions,
szInitialDirectory,
L"*.udl",
&szSelected);
if (hr == S_OK)
hr = OpenFromFileName(szSelected);
else if (hr == S_FALSE) // The user clicked cancel.
hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, ERROR_CANCELLED);
// Free the string here.
if(szSelected) CoTaskMemFree((void*)szSelected);
return hr;
}
HRESULT OpenFromFileName(LPCOLESTR szFileName)
{
CComPtr<IDataInitialize> spDataInit;
LPOLESTR szInitString;
HRESULT hr = CoCreateInstance(CLSID_MSDAINITIALIZE,
NULL,
CLSCTX_INPROC_SERVER,
IID_IDataInitialize,
(void**)&spDataInit);
if (FAILED(hr))
return hr;
hr = spDataInit->LoadStringFromStorage(szFileName,&szInitString);
if (FAILED(hr))
return hr;
hr = OpenFromInitializationString(szInitString);
if (FAILED(hr))
return hr;
//Done with the szInitString, so free the string here.
if(szInitString) CoTaskMemFree((void*)szInitString);
return hr;
}
To modify the header file, perform the following steps:
- Atldbcli.h is the main file that contains the implementation of ATL OLE DB consumer template classes. It is best to make a copy of this header file in your local project folder, and modify it directly there.
- Be sure to #include the new header file in your project, instead of the original Atldbcli.h file.
- To confirm that your application is using the modified version of the header file, set a breakpoint in one of the revised functions, and verify whether or not the breakpoint is hit.
STATUSMicrosoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.
Modification Type: | Major | Last Reviewed: | 12/11/2003 |
---|
Keywords: | kbBug kbConsumer kbDTL KB271926 |
---|
|