The problem was TvarRec, SetLength is using it as the size of the elements leading to "ElementCount * SizeOf(TvarRec));
TVarRec is size of 8 bytes. Basically this explains why array of classes would work and array of records would not work if the record was larger than 8 bytes.
A class is a pointer of 4 bytes, or 8 bytes in 64 bit mode ! LOL, so it happened to fit.
Anyway solving this problem is beyond the scope of this quick posting, but I did manage to write some better TrySetLength functions but it is a very large topic.
Some ideas include DynArraySetLength or using Generic Methods or Generic classes and open array parameters or array parameters to mimic "the compiler magic" that is involved with SetLength.
Check skybuck2000 youtube account to find these videos.
One video is here, but there are others:
I spent a whole day on this, so can't sum it up in a single posting lol, then again maybe, but not gonna do it:
It will group these 3 videos under the term: (Fri13th Part x of 3) to give some order to these video, and it's kinda funny cause it's Friday the 13th of january 2023 ! ;)
(Fri13Th Part 1 of 3) Delphi: Exploring the mysterious SetLength function and maybe TrySetLength problems...
https://youtu.be/rraFJfMtWXM
(Fri13Th Part 2 of 3) Delphi: Does dynarraydim exist ?
https://youtu.be/9WlIZfRPD9Q
(Fri13Th Part 3 of 3) Delphi: TrySetLength continued, DynArrayDim does exist ! =D
https://youtu.be/5MxN_GzZV_A
One more thing, this posting is related to it, and contains some more information/context about the problem/solution:
Delphi 10.3 run button leads to strange compile/build failure:
https://groups.google.com/g/alt.comp.lang.borland-delphi/c/7UpulvwsKAE
I also have some backwards compatible solution, but it requires passing the TypeInfo(TSomething), DimensionCount, [Size1,Size2,Size3] parameters.
Bye for now,
Skybuck.