I have a variable in a procedure that I need to keep alive until an anonymous method in that procedure runs, but I don't use the variable in the anonymous method. Is there an idiomatic way to tell the compiler to capture the variable anyway?
For example:
procedure ForceCapture(const AVar);
begin
// No-op
end;
procedure TMyClass.MyProcedure;
var
Rec: TSearchRec;
begin
CallAnonMethod(@Rec,
procedure(retVal: Integer);
begin
ForceCapture(Rec); // What should this be?
if retVal = 0 then ...
end;
end;
The above works on Win32, but I'm worried that the LLVM backend or some future optimization will recognize that ForceCapture doesn't do anything and remove the capture as a no-op. In this instance, CallAnonMethod must take a pointer, not a reference, so making a copy within it isn't an option.