<div dir="auto"><div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Hairy Pixels via fpc-pascal <<a href="mailto:fpc-pascal@lists.freepascal.org">fpc-pascal@lists.freepascal.org</a>> schrieb am Mo., 18. März 2024, 13:30:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
<br>
> On Mar 18, 2024, at 5:29 PM, Michael Van Canneyt via fpc-pascal <<a href="mailto:fpc-pascal@lists.freepascal.org" target="_blank" rel="noreferrer">fpc-pascal@lists.freepascal.org</a>> wrote:<br>
> <br>
> Of course there must be, that's the whole point of copy-on-write.<br>
> <br>
> As soon as one reference is changed, a copy is made if the reference count<br>
> is larget than 1, and this copy is changed.<br>
<br>
Oh, it does copy on write. I see now that by looking at the pointer addresses.<br>
<br>
So what happens if you do this? s2 is pointing to s1 but then you change the size of s1 and ReAllocMem gets called and invalidates the pointer to the original s1. How does s2 know this and not access corrupted memory now?<br>
<br>
  s1 := '123';<br>
  s2 := s1;<br>
  s1 := 'xxxxxxxxxxxx';<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto">Your description is inaccurate: s2 does not point to s1. Instead both point to the string data with a reference count of 2. If you now change one of the two (e.g. with SetLength) then a copy of the string data will be created and that copy will be manipulated. </div><div dir="auto">The memory of the string data will only be reallocated if only one reference to that data exists. </div><div dir="auto"><br></div><div dir="auto">Regards, </div><div dir="auto">Sven </div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
</blockquote></div></div></div>