No warning/hint for newly created interface instances as const parameters
Original Reporter info from Mantis: eny
-
Reporter name: Eddy
Original Reporter info from Mantis: eny
- Reporter name: Eddy
Description:
When an object is passed as an interface reference to a procedure/function and the parameter is a const interface parameter, then the object is not freed. The reference counting goes awry. This creates memory leaks.
The main problem: the compiler does not give a warning (a hint is not sufficient) so this goes unnoticed and leads to difficult to track memory leakage bugs.
Steps to reproduce:
Create an interface class that inherits from TInterfacedObject.
Make a procedure that has this interface as a const parm.
Call the procedure where the interfaced object is created during the method call.
E.g.:
IDemoInterface = interface
procedure DoStuff;
end;
TDemoClass = class(TInterfacedObject, IDemoInterface)
procedure DoStuff;
end;
TAnObject = class
procedure DoSomething(const pIntf: IDemoInterface);
end;
//....
var obj: TAnObject;
//...
obj.DoSomething( TDemoClass.Create ); // <-- The leakage happens here
Additional information:
The added sourcecode contains a very simple demo program that demonstrates the problem.
Mantis conversion info:
- Mantis ID: 19503
- OS: XP
- OS Build: 2002; SP3
- Build: actually 2.4.4 (not in the list)
- Platform: Windows
- Version: 2.4.3
- Monitored by: » Ask (Alexander S. Klenin), » crossbuilder (Burkhard Carstens), » @MageSlayer (Denis Golovan)