TInterfaceList.IndexOf function can cause reference counted Intefaced Objects to be destroyed.
Original Reporter info from Mantis: bryan.wells
-
Reporter name: Bryan Wells
Original Reporter info from Mantis: bryan.wells
- Reporter name: Bryan Wells
Description:
The TInterfaceList.IndexOf function causes the passed in objects reference count to be incremented and then decremented. If the passed in object's reference count is already at zero (say because the object is still in the process of being initialised before assignment to a IInterface based field), then when passed to indexOf the destructor is called.
This is caused by the function prototype for TInterfaceList.Indexof being:
function IndexOf(item : IUnknown) : Integer;
The Delphi equivalent is:
function IndexOf(const Item: IInterface): Integer;
The key difference here being the 'const' which does not cause the reference count to change.
This leads to an incompatibility between FPC and Delphi behaviours.
Steps to reproduce:
Run the attached program, the output is:
Creating logger
Creating
Calling indexof
Destroying
Calling init
This should show the number 1 -> 0
(This may be accompanied by an AV as well)
What would be exptected is:
Creating logger
Creating
Calling indexof
Calling init
This should show the number 1 -> 1
Mantis conversion info:
- Mantis ID: 36136
- OS: Windows and Linux
- Platform: i386
- Version: 3.0.4
- Fixed in version: 3.3.1
- Fixed in revision: 43138 (#e89383a1)
- Target version: 3.2.0