Nested comparator function fails TStringList.CustomSort using Delphi XE3 64-bit -
this question has answer here:
i have simple code
procedure tform2.btn1click(sender: tobject); var s : tstringlist; function compare(s : tstringlist; i1, i2 : integer) : integer; begin result := comparetext(s[i1], s[i2]); end; begin s := tstringlist.create; try s.add('s1'); s.add('s2'); s.add('s3'); s.customsort(@compare); s.free; end; end;
it works expected when compile 32-bit, access violation when use 64-bit. 64-bit version in function compare, s = nil. i2 = random value
;
it works expected win64 target, if extract compare
function outside of btn1click
function.
is bug of system.classes, there way fix?
local nested functions should not assigned procedural variables (in particular should not passed procedural variable parameters).
http://docwiki.embarcadero.com/radstudio/xe4/en/procedural_types - search "nested".
the reason simple: local functions should arrange stack access of upper functions (parents) stack frames. stack frames can not exist when function jumped without prior calling of chain of parent functions 1 one. , "out of blue" dive happens when passing address outside executives oblivious particular calling chain. calling object's virtual methods without vmt having link parent classes , without self pointing proper vmt.
the bug not such code not work in win64.
the bug compiles in win32 or win64 regardless.
when delphi fix bug, no more compile such code unless make compare
proper global function should be.
Comments
Post a Comment