Search code examples
delphidelphi-xe

Are generic TObjectlist operations Delete and Move typesafe for descendants?


I have TKeyframe as a class, and Keyframes: TObjectlist<TKeyframe> in a base class TTrack, but in descendants of TTrack, Keyframes contains descendants of TKeyframe with additional fields and appropriate typecasting.

TTrack has methods that call Keyframes.Move and Keyframes.Delete methods, that generally seem to be working properly with the descendants of TKeyframe. Deleting a keyframe that isn't at the end of the list appears to be working properly except for a specific situation, when one of the additional fields in the next Keyframe is getting set to a NaN by some subsequent operation that I haven't been able to isolate.

The source of TList Delete uses System.Move to move what are just pointers for a class, so it looks safe to me. So is it safe to use with descendants of T or not? TList Move casts to T for the item being moved, so looks dodgier, but I haven't had any problems with it so far.


Solution

  • Yes, there is nothing in TObjectList<T> that affects polymorphism and System.Move just moves memory.

    To find out what code sets some field to an unexpected value I suggest to use a data breakpoint.