manager.free causes issues

Using TMS Aurelius to create a DataSnap server. As per now i have 1 functions in the server:



function TMyEntryClass.GetPerson(lID: integer): TPerson;

var

p: TPerson;

begin

p:= PersonModel.GetPerson(lID);

Result := p;

end;



In my PersonModel I have this function:





function GetPerson(lID: integer): TPerson;

var

p: TPerson;

MyConnection: IDBConnection;

SQLConn: TSQLConnection;

manager: TObjectManager;

begin

SQLConn := GetOpenSQLConnection();

MyConnection := TDBExpressConnectionAdapter.Create(SQLConn, False);

manager := TObjectManager.Create(MyConnection);

p := manager.Find<TPerson>(lID);

CloseSQLConnection(SQLConn);

Result:= p;

end;





Now, i would assume that adding 'manager.free' before returning the result is important. However, when i do, the 'p' Object is reset (all Properties set back to default values) . So the person i got from the DB, that has lastname and firstname set, now has LastName = '' etc. And if i don't (code works as-is), i get a memory leak that builds up on my server over time (I assume its the manager Object getting recreated but not disposed). Anyone know the correct method for doing this? Manual does not specify.



Creating the manager is not an expensive operation. It's better to create it

in each request than keeping one instance alive during the session
lifetime.
What you need here is just a knowledege of how manager works and
then choose
what's best for you, there is not a better practice.

When
the manager is destroyed, the objects owned by it are destroyed. Thus,
the
reason why the TPerson object is destroyed. You can just set OwnsObjects

property to false to avoid manager to destroy the
object:

manager.OwnsObjects := false;

Then you must handle the
object destruction yourself.
Another option, is to convert the object to json
before destroying the
manager, so that since you have the json
representation, you can safely
discard the object.

Thanks! That's Perfect. Figured there was an owning issue, silly me for not finding the .OwnsObjects property.