Actually I found that behaviour a few years back. I believe the clarifying sample in the doc was added after that. (Though it was correctly documented even without the sample)
It is not about this code is hard to read. (Or not meant to)
It is about knowing compiler implementation details. Something most people who click the IDE icon do not fully know.
And the point is that the internal representation of TObject (classes) as a pointer is not in the category of the very basics (though most people will find out sooner than later, and knowing it really helps.)
Also fyi there are objects that are not pointers
type
TFoo = object(TFooBase)
//....
end;
But back to the code I posted (and sorry for hijacking the topic)
Yes
txt := ''
does modify s, actually it modifies the string data to which s does point. So after that the pointer in s (ansistrings are pointers too) points to unallocated memory.
@s and @txt are different, those are the addresses of the 2 variables holding the pointer to the actual string.
Again, same as for objects @a and @b (for 2 objects after a := b) will be different, yet point to the same object data.
Since you pointed out so knowingly that FreeAndNil(a) would not set b to nil, you should have seen that @txt would no be @s, or should you not?
Btw its rhetorical, lets just say this: I have seen people making mistakes with pointers, who are way more experienced than you and me together.
So blaming the OT for his question in my mind is still inappropriate.
The different between ansistrings and objects is, that with ansistrings you can not normally run into this trouble, because they are ref counted. classes are not refcounted (at least in fpc pascal / other languages other rules)
So my code employed a trick to bypass the ref counting.
I promised the compiler, I would not touch the value, and therefore the complier skipped ref counting. Then I broke my promise (I am deeply ashamed of that).
The result is exactly the object example, I am freeing the data of the string, clearing one of 2 pointers, leaving one dangling pointer.