vrvijayaraj
Programmer
Hi there,
We have a problem in using IMalloc->Free() and CoTaskMemFree to free a dynamically allocated block of memory.
The scenario is as follows.
1) We have a COM Component (DLL) which accepts ptr to ptr of a structure and dynamically allocates an array of structure using CoTaskMemAlloc / IMalloc->Alloc
2) There is another ActiveX control which calls this COM function .The function called is executed successfully and when trying to free the memory allocated by the callee the program crashes.
Any idea why this happens (this happens when we use CoTaskMemAlloc/Free and IMalloc->Alloc/Free
Any suggestion is highly appreciated
Thanks in advance
Structure
struct _SCodecProfile
{
NL_ID idCodec; // Codec Identifier
BSTR wszName; // name for codec
long u32Version; // Codec Version
BSTR wszVerInfo;// ver info string
} SCodecProfile;
COM Component Code
STDMETHODIMP CNLClient::EnumDecoders(SCodecProfile **ppSCodecProfile, long *pnCards)
{
SCodecProfile *pCP;
IMalloc *pIMalloc=NULL;
*ppSCodecProfile=pCP=NULL;
DWORD dwNumItems=3;
HRESULT hr=CoGetMalloc(1,&pIMalloc);
if (SUCCEEDED(hr))
{
//*ppSCodecProfile=pCP=(SCodecProfile *)CoTaskMemAlloc(sizeof(SCodecProfile) * dwNumItems);
*ppSCodecProfile=pCP=(SCodecProfile *)pIMalloc->Alloc(sizeof(SCodecProfile) * dwNumItems);
if(pCP == NULL) goto val_error;
pCP[0].idCodec=1;
pCP[0].wszName=SysAllocString(_T("KFir1\0");
pCP[0].u32Version=12;
pCP[0].wszVerInfo=SysAllocString(_T("Version 12\0");
pCP[1].idCodec=2;
pCP[1].wszName=SysAllocString(_T("KFir2\0");
pCP[1].u32Version=22;
pCP[1].wszVerInfo=SysAllocString(_T("Version 22\0");
pCP[2].idCodec=3;
pCP[2].wszName=SysAllocString(_T("KFir3\0");
pCP[2].u32Version=3;
pCP[2].wszVerInfo=SysAllocString(_T("Version 33\0");
pCP[3].idCodec=4;
pCP[3].wszName=SysAllocString(_T("KFir4\0");
pCP[3].u32Version=4;
pCP[3].wszVerInfo=SysAllocString(_T("Version 44\0");
*pnCards=dwNumItems;
pIMalloc->Release();
return S_OK;
val_error:
//if(pCP) CoTaskMemFree(pCP);
if(pCP) pIMalloc->Free((LPVOID)pCP);
pIMalloc->Release();
*ppSCodecProfile= NULL;
return E_OUTOFMEMORY;
}
}
Caller Code - The caller is an ActiveX control
HRESULT CAxClient::InitClient()
{
long nDecoderCount=0;
long nStreamCount=0;
long nIPCount=0;
SCodecProfile *DecoderProfile=NULL;
HRESULT hr=S_OK;
IMalloc *pIMalloc=NULL;
hr=CoGetMalloc(1,&pIMalloc);
if ( SUCCEEDED(hr))
{
COMTRY(hr,m_pNLClient->EnumDecoders(&DecoderProfile,&nDecoderCount));//returns the values correctly
pIMalloc->Free(&DecoderProfile); //#### Program Crashes here
}
return hr;
}
Thanks in Advance...
Vrvijayaraj
We have a problem in using IMalloc->Free() and CoTaskMemFree to free a dynamically allocated block of memory.
The scenario is as follows.
1) We have a COM Component (DLL) which accepts ptr to ptr of a structure and dynamically allocates an array of structure using CoTaskMemAlloc / IMalloc->Alloc
2) There is another ActiveX control which calls this COM function .The function called is executed successfully and when trying to free the memory allocated by the callee the program crashes.
Any idea why this happens (this happens when we use CoTaskMemAlloc/Free and IMalloc->Alloc/Free
Any suggestion is highly appreciated
Thanks in advance
Structure
struct _SCodecProfile
{
NL_ID idCodec; // Codec Identifier
BSTR wszName; // name for codec
long u32Version; // Codec Version
BSTR wszVerInfo;// ver info string
} SCodecProfile;
COM Component Code
STDMETHODIMP CNLClient::EnumDecoders(SCodecProfile **ppSCodecProfile, long *pnCards)
{
SCodecProfile *pCP;
IMalloc *pIMalloc=NULL;
*ppSCodecProfile=pCP=NULL;
DWORD dwNumItems=3;
HRESULT hr=CoGetMalloc(1,&pIMalloc);
if (SUCCEEDED(hr))
{
//*ppSCodecProfile=pCP=(SCodecProfile *)CoTaskMemAlloc(sizeof(SCodecProfile) * dwNumItems);
*ppSCodecProfile=pCP=(SCodecProfile *)pIMalloc->Alloc(sizeof(SCodecProfile) * dwNumItems);
if(pCP == NULL) goto val_error;
pCP[0].idCodec=1;
pCP[0].wszName=SysAllocString(_T("KFir1\0");
pCP[0].u32Version=12;
pCP[0].wszVerInfo=SysAllocString(_T("Version 12\0");
pCP[1].idCodec=2;
pCP[1].wszName=SysAllocString(_T("KFir2\0");
pCP[1].u32Version=22;
pCP[1].wszVerInfo=SysAllocString(_T("Version 22\0");
pCP[2].idCodec=3;
pCP[2].wszName=SysAllocString(_T("KFir3\0");
pCP[2].u32Version=3;
pCP[2].wszVerInfo=SysAllocString(_T("Version 33\0");
pCP[3].idCodec=4;
pCP[3].wszName=SysAllocString(_T("KFir4\0");
pCP[3].u32Version=4;
pCP[3].wszVerInfo=SysAllocString(_T("Version 44\0");
*pnCards=dwNumItems;
pIMalloc->Release();
return S_OK;
val_error:
//if(pCP) CoTaskMemFree(pCP);
if(pCP) pIMalloc->Free((LPVOID)pCP);
pIMalloc->Release();
*ppSCodecProfile= NULL;
return E_OUTOFMEMORY;
}
}
Caller Code - The caller is an ActiveX control
HRESULT CAxClient::InitClient()
{
long nDecoderCount=0;
long nStreamCount=0;
long nIPCount=0;
SCodecProfile *DecoderProfile=NULL;
HRESULT hr=S_OK;
IMalloc *pIMalloc=NULL;
hr=CoGetMalloc(1,&pIMalloc);
if ( SUCCEEDED(hr))
{
COMTRY(hr,m_pNLClient->EnumDecoders(&DecoderProfile,&nDecoderCount));//returns the values correctly
pIMalloc->Free(&DecoderProfile); //#### Program Crashes here
}
return hr;
}
Thanks in Advance...
Vrvijayaraj