[fpc-devel] Redefine FillChar() to use out parameter instead

Jonas Maebe jonas.maebe at elis.ugent.be
Thu Nov 19 10:10:18 CET 2009


Alexander Klenin wrote on do, 19 nov 2009:

> On Thu, Nov 19, 2009 at 18:36, Jonas Maebe <jonas.maebe at elis.ugent.be> wrote:
>
>> More importantly, another thing it would affect is that in case someone has
>> written their own fillchar with an out parameter, and *expects* reference
>> counted structures to be finalised at the caller side before they are
>> overwritten with zeroes by their routine. Changing that behaviour woud
>> a) result in memory leaks when using such a routine
>> b) not be fixable without manually adding finalize calls everywhere this
>> custom fillchar routine is called, since the required typeinfo is not
>> available at the callee side
>
> This does indeed sound like an horrible hack for me, but I do not
> understand the problem fully.

It's not a hack at all, it's relying on 100% defined behaviour.

> Can you give an example code which would be (badly) affected by  
> proposed change?

type
   tr = record
     str: ansistring;
   end;

procedure clear(out rec; size: ptruint);
begin
   fillchar(rec,size,0);
end;

var
   r: tr;
begin
   r.str:=paramstr(0);
   clear(r);
end.

If you change the behaviour of "out" so that such parameters are no  
longer finalised before the routine is entered, then the above will  
cause a memory leak. You can verify this by changing the "out rec"  
parameter into "var rec".


Jonas

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.




More information about the fpc-devel mailing list