[fpc-pascal]Free Instance

Marco van de Voort marcov at stack.nl
Wed Jan 29 12:32:36 CET 2003


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



More information about the fpc-pascal mailing list