Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Sending a TStringList structure to an Android client using Datasnap


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 0 Threads: [ Previous | Next ]
Paul McManus

Posts: 11
Registered: 3/9/07
Sending a TStringList structure to an Android client using Datasnap  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 9, 2015 7:54 AM
I have a data structure which starts with a TStringlist and has elements that are strings with other TStringlist objects repeated to several levels. I need to send this data structure to an Android client which will modify it and send the result back. I already have the server and client working sending single string data in both directions successfully.

My efforts with a TStringlist seem to show that the top level list is sent and then destroyed after the first access. I'm also getting a memory leak reported on the server for all objects below the top level stringlist on each access. Clearly my understanding of how to do this is wrong. I get the impression that this should work but clearly my method is flawed. Researching this on the web is confusing as anything other than simple types seem to be handled via JSON format rather than native TObject descendants.

In case this is useful, my server method is defined like this:

function TServerMethods1.GetList(key: String): TStringList;


Creating any object other than the returned Stringlist in this function causes a memory leak. If I'm responsible for disposing of any child objects then where can that be done safely?

Please can anybody tell me the correct way to achieve this type of transfer or point me to some documentation that explains it?

Thanks.

Update:
I've done some more research into this and can confirm that the list of lists data structure is being converted to JSON correctly when tested using the web interface. It is also arriving at the client and I can access it. I am still getting the memory leak, though.

To illustrate my problem more clearly: this code snippet works OK with no memory leaks:
function TServerMethods1.GetList(key: String): TStringList;
var
  sl1: TStringList;
begin
  sl1 := TStringList.Create;
  sl1.Add(key);
  sl1.Add('Item 1.1');
  sl1.Add('Item 1.2');
  sl1.Add('Item 1.3');
  sl1.Add('Item 1.4');
 
  Result := sl1;
end;


this one causes a memory leak as sl2 is not freed.

function TServerMethods1.GetList(key: String): TStringList;
var
  sl1, sl2: TStringList;
begin
  sl2:=TStringList.Create;
  sl2.Add('Item 2.1');
  sl2.Add('Item 2.2');
  sl2.Add('Item 2.3');
 
  sl1 := TStringList.Create;
  sl1.Add(key);
  sl1.Add('Item 1.1');
  sl1.Add('Item 1.2');
  sl1.Add('Item 1.3');
  sl1.Add('Item 1.4');
  sl1.AddObject('Item 1.5', sl2);
 
  Result := sl1;
end;


I now understand that freeing the returned object after the first access is by design, although I can find anywhere that explains this. Any child objects referenced by the returned top level object are parsed and converted to JSON correctly, however they are simply abandoned as the code only frees this top level object.

My question now becomes:

What is best practice in this case regarding memory management:
1. Is there a "safe place" where, after the data has been sent to the client, I can add code to free the additional data structure?
2. Create a descendant class and write my own marshalling code?
3. Or some other method?

Edited by: Paul McManus on Oct 13, 2015 7:01 AM
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02