[Lazarus-es] Duda sobre funcion free

JoshyFun joshyfun en gmail.com
Mar Jul 28 11:16:31 CEST 2009


Hello Jose,

Tuesday, July 28, 2009, 9:38:05 AM, you wrote:

JAC> Hola, alguien me puede indicar si es mejor usar "FreeAndNil" o 
JAC> "FreeThenNil".
JAC> Gracias y un saludo.

Compara los códigos:

FreeAndNIL:

procedure FreeAndNil(var obj);
  var
    temp: tobject;
  begin
    temp:=tobject(obj);
    pointer(obj):=nil;
    temp.free;
  end;

FreeThenNIL:

procedure FreeThenNil(var AnObject: TObject);
begin
  if AnObject<>nil then begin
    AnObject.Free;
    AnObject:=nil;
  end;
end;

Y creo que el "help" del fpc aclara la duda:

"FreeAndNil will free the object in Obj and will set the reference in
Obj to Nil. The reference is set to Nil first, so if an exception
occurs in the destructor of the object, the reference will be Nil
anyway."

-- 
Best regards,
 JoshyFun





More information about the Lazarus-es mailing list