Strange issue using Aurelius entity in TObjectDictionay with [doOwnsValues]

Hi, using Delphi 10.4.2 and Aurelius 5.8.0.0 I have a strange pointer error that I'm unable to figure out. I might be overlooking something very obvious here, but maybe someone can have a look at the code below. Please see the comments in the code to demonstrate the issue.

I'm basically adding some TEntities to a TObjectDictionary, and the TObjectDictionary has the [doOwnsValues] flag set to free it's Values (Objects) at destruction.
In my example I'm using a TCustomer entity, nothing special with the entity, just string and integer fields, but some of the string fields are Nullable. I've tried several other entities from my Model with the same result. It seems to only happen if I use one of my Aurelius Model entities, strange.

var
  LClient: TXDataClient;
  LCustomer: TCustomer;
  LCustomerList: TList<TCustomer>;
  LSortString: string;
  odEntityList: TObjectDictionary<string, TCustomer>;
begin
  odEntityList := TObjectDictionary<string, TCustomer>.Create([doOwnsValues]);
  try
    LClient := TXDataClient.Create;
    try
      LClient.Uri := 'http://LocationOfYourApis';
      LSortString := '&$orderby=ID';
      LCustomerList := LClient.List<TCustomer>(LSortString);
      try
        // Example 1: Using this code will make the final "odEntityList.Free" raise an exception
        for LCustomer in LCustomerList do begin
          odEntityList.Add(LCustomer.ID, LCustomer);
        end;

        // Example 2: Use this code instead of Example 1 code to have see "odEntityList.Free" work 
        // as expected. PS: This code makes no sense in production of course, it's just an example to 
        // demonstrate the issue reported.
        //odEntityList.Add('1', TCustomer.Create);
        //odEntityList.Add('2', TCustomer.Create);
        //odEntityList.Add('3', TCustomer.Create);
      finally
        LCustomerList.Free;
      end;
    finally
      LClient.Free;
    end;
  finally
    odEntityList.Free; //<- **See Exception here when Example 1 code is used**
  end;
end;

Thank you in advance,
Leo

SOLVED: Just as I posted this I found the solution. My TObjectManager owns the entity objects, so this will need to be handled when adding to TObjectDictionary. So nothing wrong with Aurelius, just me learning...sorry.

Feel free to delete my post.

Leo

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.