danielj040300
asked on
InvokeHelper, VARIANT memory leak
I placed an ActiveX control into a visual C++ dialog, a interface file was build. I modified the interface from:
void CVGraph::DrawXYGraph(LPCTS TR Data)
{
static BYTE parms[] = VTS_BSTR;
InvokeHelper(0x2, DISPATCH_METHOD, VT_EMPTY, NULL, parms, Data);
}
to:
HRESULT CVGraph::DrawXYGraph(LPCTS TR Data)
{
static BYTE parms[] = VTS_BSTR;
VARIANT varhr;
InvokeHelper(0x2, DISPATCH_METHOD, VT_VARIANT, (void*)&varhr, parms, Data);
return varhr.lVal;
}
because I need to know the return value. But it causes a memory leak. How can I get rid of the memory leak or even better what is the correct way of doing this.
The function in the ActiveX control is as follow:
STDMETHODIMP CVGraph::DrawXYGraph(BSTR bstrData)
{
if( !processString( bstrData ) )
return S_FALSE;
DrawGraph();
return S_OK;
}
Thanks very much.
Daniel
void CVGraph::DrawXYGraph(LPCTS
{
static BYTE parms[] = VTS_BSTR;
InvokeHelper(0x2, DISPATCH_METHOD, VT_EMPTY, NULL, parms, Data);
}
to:
HRESULT CVGraph::DrawXYGraph(LPCTS
{
static BYTE parms[] = VTS_BSTR;
VARIANT varhr;
InvokeHelper(0x2, DISPATCH_METHOD, VT_VARIANT, (void*)&varhr, parms, Data);
return varhr.lVal;
}
because I need to know the return value. But it causes a memory leak. How can I get rid of the memory leak or even better what is the correct way of doing this.
The function in the ActiveX control is as follow:
STDMETHODIMP CVGraph::DrawXYGraph(BSTR bstrData)
{
if( !processString( bstrData ) )
return S_FALSE;
DrawGraph();
return S_OK;
}
Thanks very much.
Daniel
You wo't get the HRESULT this way.
If the method implementation returns an error code, InvokeHelper will throw an exception (COleException). You need to use the MFC Style exception handling macros (TRY, CATCH, AND_CATCH, END_CATCH) for this.
Note that, for IDispatch interfaces, "success codes" other than S_OK are not defined (or valid).
Peter
If the method implementation returns an error code, InvokeHelper will throw an exception (COleException). You need to use the MFC Style exception handling macros (TRY, CATCH, AND_CATCH, END_CATCH) for this.
Note that, for IDispatch interfaces, "success codes" other than S_OK are not defined (or valid).
Peter
ASKER
Hi,
I do get a HRESULT!! You see the problem is that it leaks memory.
Are there any other or better ways of getting the HRESULT?
Could I use something else than InvokeHelper.
Could you please give an example on how to use the TRY, CATCH, AND_CATCH, END_CATCH.
Thanks.
Daniel.
I do get a HRESULT!! You see the problem is that it leaks memory.
Are there any other or better ways of getting the HRESULT?
Could I use something else than InvokeHelper.
Could you please give an example on how to use the TRY, CATCH, AND_CATCH, END_CATCH.
Thanks.
Daniel.
how do you have amemory leak - I have use InvokeHelper for a long time and I have never known it to leak - it tidies up any BSTRs it creates before throwing the exception
also it iwll thriough an exception on an erro and terefore will not fill in your variant
see what happens if you
VARIANT varhr;
VariantInit(&varhr);
InvokeHelper(...);
example of try catch
try
{
InvokeHelper(...);
}
catch(COleException *e)
{
// hresult is in e->m_sc
}
also it iwll thriough an exception on an erro and terefore will not fill in your variant
see what happens if you
VARIANT varhr;
VariantInit(&varhr);
InvokeHelper(...);
example of try catch
try
{
InvokeHelper(...);
}
catch(COleException *e)
{
// hresult is in e->m_sc
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks it worked!!
It was what I was looking for.
Daniel.
It was what I was looking for.
Daniel.
> MFC uses it's own exception handling mechanism, so it's
but you can use C++ exception handling - the MFC handling is just a MACRO wrapper around the C++ exception handling
but you can use C++ exception handling - the MFC handling is just a MACRO wrapper around the C++ exception handling
Yes I know they changed the implementaiton to use C++ exception handling in I think MFC 3.x, but as much as I remember the docs, MFC still does some "extra behind the scenes".
IMHO you need check what variant type method returns and if it BSTR you need destroy it by SysFreeString