Well,
no, not cool, at least not optimal:
Both the Tedit and the string are managed types, indeed, but:
The parameters should be passed as
const to prevent the reference count to increase:
class function MTextEd.GetVal(const te: TEdit; const regPath: string): string;
begin
WriteToRegistry(te.Text, Concat('SOFTWARE\CustomOps', regPath));
Result := te.Text; // Result is available. Use it! instead of name may avoid ambiguity later
end;
Without the
const modifier you can - not always - indeed create memory leaks if you are not careful.
It prevents copy on write semantics, which causes the refcount to go up, so it is safer and much faster code.
Without
const/
constref or
var you are actually working with local copies!
Example to see the effect:
{$mode delphi}{$H+}
function test1(t:string):integer;
begin
Result := StringRefCount(t);
end;
function test2(const t:string):integer;
begin
Result := StringRefCount(t);
end;
var
s:string = 'test me';
begin
writeln(test1(s+'1')); // prints 2
writeln(test2(s+'2')); // prints 1
end.
The effect on the TEdit is of course the same as above.
Note here there is no leak, but it is easy to make it leak.