Solved

InvokeHelper, VARIANT memory leak

Posted on 2001-08-01
8
1,256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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 …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

691 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