[fpc-pascal] Read-only global references

Mark Morgan Lloyd markMLl.fpc-pascal at telemetry.co.uk
Sun Mar 6 10:14:11 CET 2011


Marcos Douglas wrote:

>>> Make a function to return a global variable does not make it read only.
>>> If I use your function like this:
>>> o := InitText;
>>> o := nil;  //this is the same xInitText = nil
>> Disagree. You are changing the value of o, not of xInitText. xInitText is
>> initialised once somewhere in the unit exporting it, subsequently its
>> properties etc. can be used elsewhere e.g. InitText.AppendFormatted(...).
> 
> I was right.
> The "o" points to "xInitText". If you release "o" the "xInitText" will
> be release too.
> But if you do:
> o:=nil
> ...isn't the same xInitText:=nil, of course.

You're right if you're talking about primitive heap operations, in 
exactly the same way that you'd be right if you used assembler to 
trample all over flat memory. However sticking to my original question 
which dealt with exporting an instance of an object, you could free the 
read-only object by doing o.Free which is identical to InitText.Free, 
i.e. you'd be using the declared destructor- which presumably could be 
hidden from calls outside the exporting unit.

-- 
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]



More information about the fpc-pascal mailing list