Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CoTaskMemAlloc, IMalloc Crash 1

Status
Not open for further replies.

vrvijayaraj

Programmer
Jun 11, 2000
19
JP
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
 
Seems to me you should leave the & out of

pIMalloc->Free(&DecoderProfile);



:) Hope that this helped! :)
 
We have tried that also..
Even then it crashes...
Thanks...
 
Then (after a slightly closer look at your code) it's most likely because you only allocate space for three SCodecProfile structs (dwNumItems=3;) but use four. (And the & should not be there!)

BTW, when you delete the array you're leaving two BSTRs unfreed for each entry in your array. I'm not sure also why you don't just use new [] and delete [] and maybe make a class object out of the struct which would free the strings in the destructor. And it's a like time since I saw a goto used in a COM program! :) Hope that this helped! ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top