Board index » cppbuilder » BSTR returned from ActiveX leaks

BSTR returned from ActiveX leaks

Hi
I am using C++Builder 5.0 with upgrad pack 1.
When i call the function below (lp->get_Name()) residing in an third party
ActiveX component, the program loose memory.

The function I am calling looks like this:

//**************************
  BSTR /*[VT_BSTR:0]*/ __fastcall get_Name()
//**************************

I call the function like this:

for (int i = 0; i < 1000000; i++) {
  for (int j = 1; j <= Map1->Layers->Count; j++) {
    LayerPtr lp = Map1->Layers->Item(j);
    bstr = lp->get_Name(); //receiving the name in an BSTR
    SysFreeString(bstr); //freeing the memory allocated by the BSTR
  }

Quote
}

Note that this is only a test to se the memory leak clearly.

The method (lp->get_Name()) is in an ActiveX control (MapX) made by MapInfo
and thus I don't
know if they have used MS VC to make it. It looks like all functions in the
component where a BSTR is returned leaks. I have asked MapInfo but the say
they don't support C++Builder and that there is no leak in VC when calling
the exact same function, I have tested it in VB my self and the component
works fine
there.

When I comment the lines below there is no leak:
   // bstr = lp->get_Name(); //receiving the name in an BSTR
   // SysFreeString(bstr); //freeing the memory allocated by the BSTR

Is there another way to receive BSTR's that doesn't leak memory, or am I
doing something wrong?

Best regards,

Harald Hjelmseth

 

Re:BSTR returned from ActiveX leaks


Harald,

I believe the memory leak is not from the string, but is occurring because
of Borland's wrapper objects(*Ptr classes).  There was a long thread a while
back related to these issues.  You should also use the WideString class, it
should take care of freeing the string properly.

I think I would to use retrieve the layers object, then get the layer
object, always typcasting to (IDispatch*), you may also need an update to
uticls.h to do the typecasts.

// asuming there is a LayersDisp and LayerDisp class in this OCX
WideString wName;
LayersDisp layers;
LayerDisp lp;
for (int i = 0; i < 1000000; i++) {
  layers = (IDispatch*) Map1->Layers;
  for (int j = 1; j <= Map1->Layers->Count; j++) {

    lp =(IDispatch*)layers->Item(j);
    wName = lp->get_Name(); //receiving the name into a WideString
  }

Quote
}

Re:BSTR returned from ActiveX leaks


Harald,

Sorry for not replying, but the thread was started by Phil Parker and it was
titled:

PLEASE READ. BUILDER COM SUPPORT BROKEN

It was a long thread.  You can try to use deja to search for it, but I don't
know if they have it since Google bought them.

After thinking about it, since you started out using BSTR and then used
SysFreeString, you would think there would be no smart interface issue.
Have you stepped into the wrapper code, and checked if there was ever a
Variant wrapping the BSTR.  If I remember there used to be an issue with
Variants and BSTRs, but I think it had to do with the Variant assuming
ownership and freeing it.

Sorry I am not much help.

Other Threads