Jump to content
Sign in to follow this  
Rainer Wolff

spring4d list immidiately gets freed

Recommended Posts

Hello,

i have the following lines:

class function TToollist.GetItemById(Index: Integer): TTool;
var
  w:TTool;
begin
  result := nil;
  for w in Toollist do
  begin
    if w.oid = Index then
      result := w;
  end;
end;

class function TToollist.GetToollist: IList<TTool>;
begin
  if not Assigned(FToollist) then
  begin
    FToollist:=TCollections.CreateObservableList<TTool>;
    FToollist.AddRange(dm.Session.FindAll<TTool>);
//    FToollist:=dm.Session.FindAll<TTool>;
  end;
  Result:=FToollist;
end;

My property Toollist is touched for the first time by fetching an Item by Id, the getter finds FToollist not yet assigned, creates it and reads its data from database, using spring4d orm.

I want the Items to be in an Observable list, so loading db and then adding to the List.

 

But as soon as I leave the getter, my FToollist is freed via interface reference count and all my items are released.

 

If I just load my items directly into the FToollist, as in the commented line instead of using CreateObservableList and adding range, everything is fine.

 

My FToollist is a IList<TTool> in both cases.

 

I'd like to learn the internals of this behaviour, can someone explain it to me?

 

 

Share this post


Link to post
Posted (edited)

FToollist is certainly not being freed but the items because the list returned by FindAll has OwnsObjects = True and goes out of scope at the end of the method.

Write dm.Session.FindAll<TTool>.MoveTo(FToolllist);

Edited by Stefan Glienke

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×