Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freeing OleVariant after using CreateOleObject

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;
like image 845
kobik Avatar asked Nov 29 '11 16:11

kobik


1 Answers

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.

like image 61
Remy Lebeau Avatar answered Sep 22 '22 23:09

Remy Lebeau