code from "Feature request: type safe FreeAndNil" causes Internal error when added to sysutils
Original Reporter info from Mantis: Thaddy
-
Reporter name: Thaddy de Koning
Original Reporter info from Mantis: Thaddy
- Reporter name: Thaddy de Koning
Description:
Ghist: https://wiert.me/2017/12/21/another-case-against-freeandnil/ and much older discussions
In FreePascal it is possible to write a fully type safe FreeAndNil using a generic procedure.
This would remove some of the objections against it.
Like adding:
procedure FreeAndNil<T:class>(var obj:T);
var
temp: T;
begin
temp := obj;
obj := nil;
temp.free;
end;
Or in objfpc mode:
generic procedure FreeAndNil<T:class>(var obj:T);
var
temp: T;
begin
temp := obj;
obj := nil;
temp.free;
end;
Steps to reproduce:
Try one of the examples where FreeAndNil is your enemy.
Mantis conversion info:
- Mantis ID: 36115
- Version: 3.3.1