"as"-operator on interfaces references temporary variable
Original Reporter info from Mantis: Martok @martok
-
Reporter name:
Original Reporter info from Mantis: Martok @martok
- Reporter name:
Description:
Using the as-Operator to cast COM-Interfaces first creates a temporary variable and then copies this over to the final location. The temp variable is kept referenced until the method's cleanup section is reached.
This causes instances to be longer lived then expected, particularly breaking (Delphi-)code when the object should be freed at some specific point, i.e. by setting a variable to nil.
Delphi does not use a temporary variable at all and rather calls IntfCast with the final target of the operation. While the example uses an assignment, the same is true for call parameters (target there is usually a reference to a register).
Steps to reproduce:
Compile the attached program and observe its output.
Results (FPC trunk):
RefCount: 3
RefCount: 2
RefCount should be 0...
Results (Delphi, expected):
RefCount: 2
RefCount: 1
RefCount should be 0...
Additional information:
Original issue is in code similar to this:
// Open File (str:IIOSeekable) and put a Decoder (dec:IIOBase64Decoder) on it
str:= TIOFileStream.Create(FileName, fmOpenRead);
dec:= TBase64Decoder.Create(str as IIOReadable);
// Clear all references, implicitly free stream and close file
dec:= nil;
str:= nil;
// file is still open, because 'str as IIOReadable' created an invisible reference!
Mantis conversion info:
- Mantis ID: 25990
- Monitored by: » @martok (Martok)