Solved

InvokeHelper, VARIANT memory leak

Posted on 2001-08-01
8
1,214 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Show shut-down message as Windows 8 shuts down. 9 99
Separate files macro - Errors on larger data set 18 73
sameEnds challenge 3 179
Is there a simple front-end menu system. 9 102
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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

789 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