Why do I get an invalid pointer in the TListSortCompare function when calling sort on a TList?
I'm adding some pointers to a TList object:

    list : TList.create();
    
    // Just an example of value adding
    while not ok do
    begin
      newValue = new(MyStructurePointer);
      newValue^.val1 := 1;
      list.add(newValue);
    end;

    // Then, I'd like to sort it
    list.sort(comparisonFunction);

My comparison function looks like:

    function comparisonFunction(p1, p2 : Pointer): integer
    begin
      if (p1 = nil) or (p2 = nil) then
      begin
        if (p1 = nil) and (p2 = nil) then result := 0
        else if (p1 = nil) then result = 1
        else result := -1;

        exit;
      end;

      result := MyStructurePointer(p2)^.val1 - MyStructurePointer(p1)^.val1;
    end;

But, when evaluating p1^.val1 or p2^.val2, one of these pointers have an **invalid value** !

Any idea ?
Why do I get an invalid pointer in the TListSortCompare function when calling sort on a TList?
===

I'm adding some pointers to a TList object:

    list : TList.create();
    
    // Just an example of value adding
    while not ok do
    begin
      newValue = new(MyStructurePointer);
      newValue^.val1 := 1;
      list.add(newValue);
    end;

    // Then, I'd like to sort it
    list.sort(comparisonFunction);

My comparison function looks like:

    function comparisonFunction(p1, p2 : Pointer): integer
    begin
      if (p1 = nil) or (p2 = nil) then
      begin
        if (p1 = nil) and (p2 = nil) then result := 0
        else if (p1 = nil) then result = 1
        else result := -1;

        exit;
      end;

      result := MyStructurePointer(p2)^.val1 - MyStructurePointer(p1)^.val1;
    end;

But, when evaluating p1^.val1 or p2^.val2, one of these pointers have an **invalid value** !

Any idea ?