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

Jonas Maebe jonas.maebe at elis.ugent.be
Thu Nov 19 22:49:27 CET 2009


On 19 Nov 2009, at 22:31, Florian Klaempfl wrote:

> Because a VAROUT parameter would be simply overwritten by the callee
> even if it contains a valid automated type:
> 
> procedure p(varout v : ansistring);
> begin
>  v:='asdf';
> end;

I don't think that this could cause a memory leak, otherwise this would cause two memory leaks:

procedure p(varout v : ansistring);
begin
 v:='asdf';
 v:=v+v;
 v:='test;
end;

The fact that v is a varout would simply mean that it would not be finalized before the call. It wouldn't necessarily mean that reference counting would no longer be applied inside the callee. Just like with regular "out" parameters the compiler inside the callee still calls FPC_DECR_REF* on the out parameter every time you assign something to it, even though it's finalized before the call.

What would cause a memory leak, is this:

procedure clear(varout v; size: ptruint);
begin
 fillchar(v,sizeof(v),0);
end;

var
 s : string;
begin
 s:='asdf';
 s:=s+s;
 clear(s,sizeof(s));
end;

But that's the same with "var".


Jonas


More information about the fpc-devel mailing list