Sign in Join

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

Mailing lists / fpc-pascal / 2003-01

Free Instance

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Luis Del Aguila

I don't Understand, I compile this code with FPC 1.06 for Windows and the directive compiler -S2 -Rintel.
is it a bug?.

Thanks

 Type
   TClase1 = Class
     campo1:string;
   End;
   TClase2 = Class (TClase1)
     campo2:string;
   End;
 Var b:Tclase2;
 Begin
   b:=TClase2.create;
   b.campo1:='hola';
   b.campo2:='hola2';
   Writeln(b.campo2);
   b.free;                          //if free the instance .
   Writeln(b.campo2);     // Why the instance exist?
   Write('presione enter para terminar');readln
 End.
Marco van de Voort
to Luis Del Aguila
Show quoted text 25 lines
I don't Understand, I compile this code with FPC 1.06 for Windows and the directive compiler -S2 -Rintel. is it a bug?. Thanks Type TClase1 = Class campo1:string; End; TClase2 = Class (TClase1) campo2:string; End; Var b:Tclase2; Begin b:=TClase2.create; b.campo1:='hola'; b.campo2:='hola2'; Writeln(b.campo2); b.free; //if free the instance . Writeln(b.campo2); // Why the instance exist? Write('presione enter para terminar');readln End.

Because the memory is released (iow it can be used for the next allocation), but the data is not erased (e.g. filled with zeroes), and the pointer still points to the data. (but that is dangerous, since after the free it _can_ be overwritten)

Use freeandnil(b); to also set it the pointer to NIL.

Andrew J.Swan [home]
to Luis Del Aguila: I don't Understand, I compile this code with FPC 1.06 for Windows and the directive…

* Luis Del Aguila [LDA] [Wednesday, January 29, 2003] wrote:
* subject: [fpc-pascal]Free Instance
*

[...]

LDA>    Writeln(b.campo2);
LDA>    b.free;                //if free the instance .
        // replace b.free with:
        FreeAndNil (b) ;
        If Assigned (b)
          Then
LDA>    Writeln(b.campo2);     // Why the instance exist?
LDA>    Write('presione enter para terminar');readln
LDA>  End.

8o)

* Best regards ...

` _ , ' :: Linux Registered User [#168882] - (o)o) - :: The-Bat! 1.63 Beta/5 -ooO'(_)--Ooo---- :: Weakened by Windows 98/4.10.2222 A - Don't waste your idle time, Donate it! [www.distributed.net] >