Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus-es / 2009-07

Duda sobre funcion free

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

JoshyFun
to Jose Antonio Cuello

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 AnObjectnil 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

Reply by email

Jose Antonio Cuello

Joer, hab�a mirado el c�digo fuente pero no lo v� claro. Est� claro que estaba dormido ... :-[
Gracias, ya lo v� ... lo dice los mismos nombres de funci�n. "FreeAndNil" y "FreeThenNil" ... :-D :-D

Show quoted text 48 lines
Message: 1 Date: Tue, 28 Jul 2009 11:16:31 +0200 From: JoshyFun <joshyfun en gmail.com> Subject: Re: [Lazarus-es] Duda sobre funcion free To: Spanish version of Lazarus List <lazarus-es en lists.lazarus.freepascal.org> Message-ID: <14610477244.20090728111631 en gmail.com> Content-Type: text/plain; charset=ISO-8859-1 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 AnObjectnil 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."

Reply by email

JoshyFun
to Jose Antonio Cuello

Hello Jose,

Tuesday, July 28, 2009, 1:48:02 PM, you wrote:

JAC> Joer, hab�a mirado el c�digo fuente pero no lo v� claro. Est� claro que JAC> estaba dormido ... :-[
JAC> Gracias, ya lo v� ... lo dice los mismos nombres de funci�n. JAC> "FreeAndNil" y "FreeThenNil" ... :-D :-D

Yap, pero lo m�s importante no es el orden en si, si no el hecho de que sucede si se produce una excepci�n al ejecutar el free, en el caso del FreeThenNIL la referencia sigue teniendo su valor original que podr�a ser deseable. FreeAndNIL asegura que la referencia ser� nil pase lo que pase en el free. Lo escribo m�s que nada, por si alguien no se habia dado cuenta, y el hecho de que FreeThenNIL es parte de la LCL de Lazarus, no de FPC.

Best regards, JoshyFun

Reply by email