Solved

InvokeHelper, VARIANT memory leak

Posted on 2001-08-01
8
1,169 Views
Last Modified: 2013-11-20
I placed an ActiveX control into a visual C++ dialog, a interface file was build. I modified the interface from:

void CVGraph::DrawXYGraph(LPCTSTR Data)
{
     static BYTE parms[] =     VTS_BSTR;
     InvokeHelper(0x2, DISPATCH_METHOD, VT_EMPTY, NULL, parms, Data);
}

to:

HRESULT CVGraph::DrawXYGraph(LPCTSTR 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
0
Comment
Question by:danielj040300
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 12

Expert Comment

by:migel
ID: 6340108
Hi!
IMHO you need check what variant type method returns and if it BSTR you need destroy it by SysFreeString
0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6342255
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
0
 
LVL 1

Author Comment

by:danielj040300
ID: 6343592
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.
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6343966
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
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Accepted Solution

by:
peterchen092700 earned 200 total points
ID: 6343998
a) How do you see it's an HRESULT?

the declaration does not indicate a dispatch return value (otherwise there would be something like STDMETHODIMP CVGraph::DrawXYGraph(BSTR bstrData, /* [out,retval] */ RETTYPE * retval). HRESULT's are (almost) never returned as dispatch return value.

Before you call InvokeHelper, call VariantInit(&varhr);
This will clear the variable (which ciontains "almost random" data) before the function is called. So you see if there's actually something written into it. If so, please post the vartype (varhr.vt) after InvokeHelper returned


b) Shaun: MFC uses it's own exception handling mechanism, so it's

TRY
{
  DrawXYGraph(LPCTSTR Data)
}
CATCH (COleException,xcept)
{
  // xcept is a COleException *
  ....
}
END_CATCH


Peter
0
 
LVL 1

Author Comment

by:danielj040300
ID: 6344084
Thanks it worked!!
It was what I was looking for.

Daniel.
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6344440
> 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  
0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6344723
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".
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now