Solved

InvokeHelper, VARIANT memory leak

Posted on 2001-08-01
8
1,192 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand data scrubbing tool 13 32
maxBlock challenge 30 129
Use of condition with 'serial' in ansible 2 60
Increment column based of a FK 8 22
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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.

777 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