Discuss low level handling of reference counted interfaces
Original Reporter info from Mantis: Adriaan van Os
-
Reporter name: Adriaan van Os
Original Reporter info from Mantis: Adriaan van Os
- Reporter name: Adriaan van Os
Description:
This bug report is a follow-up of on the thread "Reference counting interface objects" on the fpc-devel mailing list of October 16, 2014. It suggests to discuss low-level handling of reference counted interfaces in the FPC documentation.
As mentioned on the thread, in Pascal code _Release doesn't need to be called, since the purpose of reference counting is to handle this automatically. However, when interfacing with external code, the internal rules must be precisely known.
For example, when writing bindings for external interfaces, the difference between VAR en OUT becomes essential. An OUT parameter in external COM code that is declared VAR in the Pascal bindings, produces a memory leak on each call. To understand this, the low-level details must be known.
Additional information:
Jonas Maebe wrote:
Regarding the effects of the various qualifiers:
* nothing (value): the reference count of the parameter is increased (I forgot whether it's the caller or callee that does it) on entry and decreased on exit
* OUT: the reference count of the value that's passed in is decreased by 1, and the variable that's passed into the procedure is initialized to "empty" (nil, but that's an implementation detail)
* VAR: nothing happens to the reference count. A reference to the original variable is passed in, and changing it or reading it has exactly the same effect as changing/reading the original variable.
* CONST: this is the only tricky one. Again nothing happens to the reference count, but because you can pass non-lvalues here. In particular, you can pass a class implementing an interface rather than the interface itself, which can cause the class to be freed unexpectedly. See the example at http://docwiki.embarcadero.com/RADStudio/XE7/en/Using_Reference_Counting . The same can happen with functions returning a class instance passed to a function expecting a CONST interface parameter. There is an open bug report about adding a warning for it: http://bugs.freepascal.org/view.php?id=19503
Mantis conversion info:
- Mantis ID: 26874
- OS: Win32
- OS Build: WIn7
- Platform: Windows
- Version: 2.6.0
- Fixed in version: 3.0.0
- Fixed in revision: 1151 (#644b916b)
- Monitored by: » Adriaan van Os (Adriaan van Os), » dezlov (Denis Kozlov)
- Target version: 3.0.0