Question

actually i am using late-binding in delphi, and i need to know wich is the proper way to work with it.

My principal concern is about how I handle the memory used by these objects, I must free the memory?

check this sample code

var
  chEaten: Integer;
  BindCtx: IBindCtx;
  Moniker: IMoniker;
 MyObject:: IDispatch;
begin
try  
  OleCheck(CreateBindCtx(0, bindCtx));
  OleCheck(MkParseDisplayName(BindCtx, StringToOleStr('oleobject.class'), chEaten, Moniker));
  OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, MyObject));

  MyObject.Metod1();
  MyObject.Metod2();
 finally
 MyObject:=nil,// is  this necesary?
 end;

end;

would be helpful if someone explain briefly how is handled the memory in this type of objects.

thanks in advance.

Was it helpful?

Solution

COM Interface objects in Delphi are automatically managed by the compiler. It inserts hidden calls to AddRef and Release at the appropriate places, and your interfaces will automatically have their Release methods called when they go out of scope. So no, you don't have to nil out the reference.

OTHER TIPS

Like Mason said, the memory for the interfaces is managed by the compiler for you. However, StringToOleStr() returns an allocated BSTR that needs to be freed manually with SysFreeString(). You should use the WideString type instead, which manages the memory for you, eg:

OleCheck(MkParseDisplayName(BindCtx, PWideChar(WideString('oleobject.class')), chEaten, Moniker)); 

Or:

var
  w: WideString;

w := 'oleobject.class';
OleCheck(MkParseDisplayName(BindCtx, PWideChar(w), chEaten, Moniker)); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top