Freeing OleVariant after using CreateOleObject
Asked Answered
R

1

7

Here is a simple code:

procedure Test;
var
  V: OleVariant;
begin
  V := CreateOleObject('ADOX.Catalog');
  try
    // do something with V...
  finally
    V := Unassigned; // do we need this?
  end;
end;

Do we need to use the V := Unassigned code at the end, or will V is free when it exists the scope of Test procedure? in VB you set the variable to Nothing. do we need to do the same here? ie:

function VarNothing: IDispatch;
// emulate VB function SET VarX = Nothing
var
  Retvar: IDispatch;
begin
  Retvar := nil;
  Result := Retvar;
end;

// do something with V and finally:
V := VarNothing;
Rothberg answered 29/11, 2011 at 16:25 Comment(0)
B
9

OleVariant will release the interface automatically when it goes out of scope. You can assign a new value to the OleVariant if you need it to be released sooner.

Biretta answered 29/11, 2011 at 16:54 Comment(8)
How can that be verified? what happens when the OleVariant is assigned to Unassigned or Null?Rothberg
Compile with debug 'dcu's, and put a breakpoint on _VarClear procedure in variants.pasCaesium
Assigning any value to an OleVariant (or a Variant, for that matter) will automatically free whatever data the OleVariant is currently holding on to, before then assigning the new value. That includes deallocating dynamic strings/arrays, releasing interface pointers, etc.Biretta
using V := Unassigned will call VariantClear (oleaut32.dll), otherwise (doing nothing and getting out of scope) will trigger _IntfClear. I wonder why is that... and what is the best coding practice.Rothberg
The compiler does not call _IntfClear() on an OleVariant. An OleVariant is a wrapper around an OLE VARIANT record, and thus has to be managed using OLE support functions (VariantInit(), VariantClear(), etc) only. When an OleVariant goes out of scope, the compiler should be calling VariantClear(). _IntfClear() is meant for releasing Delphi-style interface variables instead, which OleVariant is not.Biretta
As for coding practice, you should let the OleVariant just go out of scope unless you need to reuse the variable, or have good reason to release/reclaim the OleVariant's memory manually.Biretta
@RemyLebeau, by "unless you need to reuse the variable" do you mean for example, if I want to reuse the same variable to point another OLE Object, I must first call `V := Unassigned', right? I'm currently fixing a "The RPC server is unavailable", and another question(vb.net calling Word) said the samething as I understand it, but not quite sure if it also applies to calling Word from Delphi.Bound
@EdwinYip depends. If you simply assign a new interface/array pointer, a previous one will be released automatically. But if you pass the variable to an external function that then assigns a new interface/array, you may have to explicitly clear the variable first.Biretta

© 2022 - 2024 — McMap. All rights reserved.