Re: [fpc-devel] ref count issue with out param

2015-06-15 Thread Den
It is similar to type tarray = array [1..10] of longint; procedure p(const a1 : tarray;var a2 : tarray); begin a2[1]:=4321; writeln(a1[1]); // surprise end; var arr : tarray; begin arr[1]:=1234; p(arr,arr); end. The only way to prevent this (of course, such a simple case could be detected by th

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Martin Frb
On 13/06/2015 21:48, Jonas Maebe wrote: On 13/06/15 22:04, Florian Klämpfl wrote: Or is it documented somewhere that out parameters are finalized first and then the other parameters are loaded? Regardless of whether it's documented, maybe we should do that anyway. It doesn't seem like a bad id

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Jonas Maebe
On 13/06/15 22:04, Florian Klämpfl wrote: > Or is it documented somewhere that out parameters are finalized first and > then the other parameters > are loaded? Regardless of whether it's documented, maybe we should do that anyway. It doesn't seem like a bad idea. Jonas

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Florian Klämpfl
Am 13.06.2015 um 21:04 schrieb Martin Frb: > On 13/06/2015 19:55, Florian Klämpfl wrote: >>> procedure Foo1(a: AnsiString; out b: AnsiString); >>> procedure Foo2(out a: AnsiString; b: AnsiString); >>> >>> So in Delphi it does not depend on this order. The "none out" is always nil. >> Yes. Implement

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Martin Frb
On 13/06/2015 19:55, Florian Klämpfl wrote: procedure Foo1(a: AnsiString; out b: AnsiString); procedure Foo2(out a: AnsiString; b: AnsiString); So in Delphi it does not depend on this order. The "none out" is always nil. Yes. Implementation specific behavior. Really. Then Delphi documented t

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Florian Klämpfl
Am 13.06.2015 um 20:08 schrieb Martin Frb: > On 13/06/2015 17:48, Florian Klämpfl wrote: >> Am 13.06.2015 um 16:42 schrieb Martin Frb: >>> On 13/06/2015 15:18, Florian Klämpfl wrote: Foo2(a1[i],a2[j]) a1, a2 being dyn. arrays, but sharing data, i and j having the same value. >>>

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Martin Frb
On 13/06/2015 17:48, Florian Klämpfl wrote: Am 13.06.2015 um 16:42 schrieb Martin Frb: On 13/06/2015 15:18, Florian Klämpfl wrote: Foo2(a1[i],a2[j]) a1, a2 being dyn. arrays, but sharing data, i and j having the same value. I dug out my old "turbo delphi". In all both cases (my original ex

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Florian Klämpfl
Am 13.06.2015 um 16:42 schrieb Martin Frb: > On 13/06/2015 15:18, Florian Klämpfl wrote: >> Foo2(a1[i],a2[j]) >> >> a1, a2 being dyn. arrays, but sharing data, i and j having the same value. >> >> > > I dug out my old "turbo delphi". > > In all both cases (my original example / you dyn array exam

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Michael Van Canneyt
On Sat, 13 Jun 2015, Michael Van Canneyt wrote: In my example the out param is fine too, but the other param (that gets passed the same param on the callee side) is dangling. Normally only if the initial reference count was 1. And now you know why reference counts can be dangerous and bit

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Martin Frb
On 13/06/2015 15:18, Florian Klämpfl wrote: Foo2(a1[i],a2[j]) a1, a2 being dyn. arrays, but sharing data, i and j having the same value. I dug out my old "turbo delphi". In all both cases (my original example / you dyn array example) Delphi passes nil for the normal parameter, and fpc pass

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Michael Van Canneyt
On Sat, 13 Jun 2015, Martin Frb wrote: On 13/06/2015 15:24, Michael Van Canneyt wrote: On Sat, 13 Jun 2015, Martin Frb wrote: On 13/06/2015 15:12, Michael Van Canneyt wrote: Note the 0 ! Secondly, the section about 'out' parameters is very old; there were not nearly so much managed t

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Martin Frb
On 13/06/2015 15:24, Michael Van Canneyt wrote: On Sat, 13 Jun 2015, Martin Frb wrote: On 13/06/2015 15:12, Michael Van Canneyt wrote: Note the 0 ! Secondly, the section about 'out' parameters is very old; there were not nearly so much managed types at the time. I will update it, I was j

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Michael Van Canneyt
On Sat, 13 Jun 2015, Martin Frb wrote: On 13/06/2015 15:12, Michael Van Canneyt wrote: Note the 0 ! Secondly, the section about 'out' parameters is very old; there were not nearly so much managed types at the time. I will update it, I was just documenting the new string types anyway. The

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Florian Klämpfl
Am 13.06.2015 um 14:27 schrieb Martin Frb: > On 13/06/2015 09:31, Florian Klämpfl wrote: >> It is similar to >> >> type >>tarray = array [1..10] of longint; >> >> procedure p(const a1 : tarray;var a2 : tarray); >>begin >> a2[1]:=4321; >> writeln(a1[1]); // surprise >>end; >>

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Florian Klämpfl
Am 13.06.2015 um 15:09 schrieb Martin Frb: > On 13/06/2015 09:31, Florian Klämpfl wrote: >> The only way to prevent this (of course, such a simple case could be >> detected by the compiler but >> one can always construct an example which works around this detection), is >> to turn off the const >

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Martin Frb
On 13/06/2015 15:12, Michael Van Canneyt wrote: Note the 0 ! Secondly, the section about 'out' parameters is very old; there were not nearly so much managed types at the time. I will update it, I was just documenting the new string types anyway. The above will nicely illustrate the point.

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Michael Van Canneyt
On Sat, 13 Jun 2015, Martin Frb wrote: "conts" param http://www.freepascal.org/docs-html/ref/refsu63.html#x166-17600014.4.4 explicitly document that ref counting is not done. (But I have no const in my example) But "out" param have no such documentation. They also do *not* state in the do

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Adriaan van Os
Martin Frb wrote: But "out" param have no such documentation. Also see the thread "Reference counting interface objects" from October 2014 and PR 26874 Regards, Adriaan van Os ___ fpc-devel maillist

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Martin Frb
On 13/06/2015 10:05, Jonas Maebe wrote: Martin Frb wrote: ebx is a temp copy of s1, but then f1 becomes nil, and ebx points to freed memory. It's because "out" finalises on the caller side (due to its COM origin, I guess) while value parameters get their reference count increased on the call

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Martin Frb
On 13/06/2015 09:31, Florian Klämpfl wrote: The only way to prevent this (of course, such a simple case could be detected by the compiler but one can always construct an example which works around this detection), is to turn off the const optimization that const allows the compiler to pass only

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Martin Frb
On 13/06/2015 09:31, Florian Klämpfl wrote: It is similar to type tarray = array [1..10] of longint; procedure p(const a1 : tarray;var a2 : tarray); begin a2[1]:=4321; writeln(a1[1]); // surprise end; var arr : tarray; begin arr[1]:=1234; p(arr,arr); end. I don

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Martin Frb
On 13/06/2015 10:05, Jonas Maebe wrote: Martin Frb wrote: ebx is a temp copy of s1, but then f1 becomes nil, and ebx points to freed memory. It's because "out" finalises on the caller side (due to its COM origin, I guess) while value parameters get their reference count increased on the call

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Jonas Maebe
Martin Frb wrote: ebx is a temp copy of s1, but then f1 becomes nil, and ebx points to freed memory. It's because "out" finalises on the caller side (due to its COM origin, I guess) while value parameters get their reference count increased on the callee side (for efficiency reasons, I guess)

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Florian Klämpfl
Am 13.06.2015 um 09:56 schrieb Sven Barth: > On 13.06.2015 06:04, Martin Frb wrote: >> program Project1; >> >> procedure Foo1(a: AnsiString; out b: AnsiString); >> begin >>WriteLn(length(a)); WriteLn(length(b)); >>b := 'a'; >> end; >> >> procedure Foo2(out a: AnsiString; b: AnsiString); >>

Re: [fpc-devel] ref count issue with out param

2015-06-13 Thread Sven Barth
On 13.06.2015 06:04, Martin Frb wrote: program Project1; procedure Foo1(a: AnsiString; out b: AnsiString); begin WriteLn(length(a)); WriteLn(length(b)); b := 'a'; end; procedure Foo2(out a: AnsiString; b: AnsiString); begin WriteLn(length(a)); WriteLn(length(b)); b := 'a'; end; c

[fpc-devel] ref count issue with out param

2015-06-12 Thread Martin Frb
Reading http://forum.lazarus.freepascal.org/index.php/topic,28740.msg180163.html#msg180163 and http://bugs.freepascal.org/view.php?id=28279 I did some tests and found something related, that I believe to be a bug. 2.6.4 and trunk (few weeks old) Could someone please confirm? The line s1 :=