[fpc-pascal] about dynamic array

Doug Chamberlin dougchamberlin at earthlink.net
Thu May 6 21:50:34 CEST 2010


On 5/6/2010 3:12 PM, José Mejuto wrote:
> Entering the doom again... Next question could be why:
> p1: TObject;
> p2: TObject;
> p1:=TObject.Create();
> p2:=p1;
> P1.free;
> p1:=nil;
>
> Why I get an AV if accesing p2 ? Because the object has been freed...
> So why is not p2 = nil as p1 ?
The short answer is because you have not set p2 to nil as you did to p1.

Pointer operations (and the disguised ones that are references to class 
instances and dynamic data structures) are NOT managed for you. You 
still have to be careful in handling them. That is why the FreeAndNil 
procedure was created. To help you manage object instance references.

Consider this: The runtime system does not keep track of how many 
variables you set pointing to a given location. To do so would be VERY 
inefficient. Therefore, it cannot go out and find all those variables 
and set them for you. As the programmer you have to do this yourself. 
That's part of what programming is all about.




More information about the fpc-pascal mailing list