Solved

InvokeHelper, VARIANT memory leak

Posted on 2001-08-01
8
1,149 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Accepted Solution

by:
peterchen092700 earned 200 total points
Comment Utility
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
Comment Utility
Thanks it worked!!
It was what I was looking for.

Daniel.
0
 
LVL 9

Expert Comment

by:ShaunWilde
Comment Utility
> 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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
repeateFront java challenge 31 87
Add content to output file 4 57
dog bark java program 15 81
matchUp  challenge 9 71
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

744 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

15 Experts available now in Live!

Get 1:1 Help Now