dispose: bug or feature?

John L. Ries jries at salford-systems.com
Mon Dec 10 17:57:29 CET 2007


On Mon, 10 Dec 2007, Scott Moore wrote:

> Russell Whitaker wrote:
>> 
>> Hi
>> Given a pointer p, dispose(p) does not set p to nil.
>> 
>> This program,
>> 
>> program tst;
>> type
>>   ptr = ^string;
>> var
>>   p : ptr;
>> begin
>>   p := new( ptr, 20 );
>>   p^ := "abc";
>>   writeln( p^ );
>>   dispose( p );
>>   p := nil;
>>   if p = nil then
>>     writeln("OK");
>>   dispose( p );
>>   writeln( p^ );
>> end.
>> 
>> as is gives you
>> 
>> abc
>> OK
>> segmentation fault
>> 
>> The seg fault is from the last writeln.
>> If you comment out the line "p := nil", the second "dispose" causes
>> a core dump.
>> If you comment out both "p := nil" and the second "dispose" you get
>> 
>> abc
>> abc
>> 
>> and that's also an error.
>> 
>> I do not know what side effects you might get if you caused "dispose"
>> to set the pointer to nil.
>>
>>   Russ
>> 
>> 
>
> In standard Pascal, dispose does not set p to nil, that's up to you.
> Calling dispose twice for the same variable will certainly cause a
> system fault. Calling dispose with nil is also an error.
>
> What did you expect to happen?
>
>
There probably wouldn't be anything wrong with GPC automatically setting 
pointers to nil when disposing them, but that would be non-standard and 
I suspect that most people (myself included) would do it themselves 
anyway (for portability's sake) unless the pointer was going to be 
immediately reassigned.


--------------------------|
John L. Ries              |
Salford Systems           |
Phone: (619)543-8880 x107 |
or     (435)867-8885      |
--------------------------|








More information about the Gpc mailing list