On 16 Oct 2014, at 15:02, Adriaan van Os wrote:
Jonas Maebe wrote:
Nothing ever needs to be manually released, regardless of whether
it's a VAR or OUT parameter. That's the whole point of automatic
reference counting)
I agree for pure Pascal code, where a parameter can be OUT or VAR
and automatic release will work in both cases. However, I disagree
for interfacing with external interfaces. It must be clear for the
bindings-writer that VAR in the bindings instead of (what should
have been) OUT can cause a memory leak. This is tricky enough that
it deserves an explanation in the manual. For example, I think (from
Windows MediaFoundation)
type
IMFMediaEventGenerator = interface( IUnknown) ...
....
function GetEvent( dwFlags: DWORD; OUT ppEvent:
IMFMediaEvent): HResult; stdcall;
is correct, whereas
function GetEvent( dwFlags: DWORD; VAR ppEvent:
IMFMediaEvent): HResult; stdcall;
will leak memory.
No, it won't:
1) if the external code expects it to be "out" and you declare it as
"var": the caller will decrease the reference count and the ppEvent
that you get will have as initial value "nil". You can just assign to
it like normal.
2) if the external code expects it to be "var" and you declare it as
"out": the caller will not do anything and hence the ppEvent will not
be nil. However, assignments to the "out" parameter still cause its
reference count to be decreased. After all, you can assign multiple
times to an "out" parameter, so the compiler cannot assume it's
already nil. The only problem that could happen here is if you would
explicitly write your code in a way that behaves differently depending
on whether the initial value is nil (e.g., assigning something to it
only in one branch of an if-statement, and then later checking whether
it's "still" nil to determine whether you have to assign something
else to it).
Jonas
_______________________________________________
fpc-devel maillist - [email protected]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel