Boian Mitov wroteI mean it was not working when the optimization was on. They did not
have option to turn the optimization off.
No, it can only work when the optimization is on in FPC. To recap:
a) in both FPC and Delphi, temps (regardless of their type) are never automatically finalized immediately after their last use, so they can retain references for a while
b) Delphi optimizes (possibly regardless of its optimization switch state) assignments of (reference counted) function results to local variables which cannot escape the current scope so that no temps are used (and hence no invisible references are created) c) FPC also optimizes these assignments as of 2.2.2, but under a few less circumstances than Delphi (see the example I posted with the nested functions)
d) in case of global variables or local variables which escape the current scope, both Delphi and FPC create temps which hold invisible references until these temps are reused for another purpose or go out of scope (per rule a).
Your programs are coded with the assumption that this optimization is always performed, which
a) is wrong for both Delphi and FPC in case global variables or local variables accessed outside their defining scope are used (as has been demonstrated using various programs posted to this mailing list) b) is wrong for FPC <= 2.2.0
c) is wrong for all current FPC versions in a few cases where the compiler can theoretically know that a local variable is not used outside its defining scope, but where it does not know that at this time
You keep however arguing that it is some mysterious optimization in FPC which causes the difference in behaviour (while it's actually a lack of a particular optimization that causes it), and that Delphi always does what you want (which it doesn't, as has been demonstrated using the example programs posted to this list).
Please understand that it is extremely tiring and frustrating to discuss in this way, because it gives the impression that you simply do not care about what causes the problem and/or simply are not listening to what we are trying to say.
So let me repeat one last time: Delphi optimizes assignments of reference counted function results to local variables which provably do not escape their defining scope so that they do not need a temp. FPC does this now also in most, but not all cases. Delphi, just like FPC, never automatically finalizes temps immediately after every statement in which they were used.
And while Delphi's optimization results in the behaviour you want for the code that you have written (and possibly the same behaviour which is explicitly defined in a number of other languages), it is pure luck that it does and it will have guaranteed different behaviour if you use global variables or local variables which can escape their defining scope. FPC does not guarantee to perform exactly the same optimizations as Delphi and therefore may also have different behaviour in a number of cases.
Jonas