Memory release on pascal

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Memory release on pascal

Postby Lena » Mon Apr 10, 2017 5:24 am

Hi.
I need to use the *.pas file in my program. I think there's an mistake In this file.
Code: Select all
procedure RegisterDevice(DeviceID : string; DeviceToken : string);
var
  // Data to be sent to the script
  postdata: TStringList;
  //Data connection
  httpconnect : TIdHTTP;
begin

  try
    // Create a connection
    httpconnect := TIdHTTP.Create;
    // Specify the data to send
    postdata := TStringList.Create;
    postdata.Add('action=register-device');
    postdata.Add('did=' + DeviceID);
    postdata.Add('token=' + DeviceToken);
    {$ifdef ANDROID}
      postdata.Add('platform=android');
    {$else}
      postdata.Add('platform=ios');
    {$endif}

    //send
    httpconnect.Post(DOMAIN + 'push.php', postdata);
  finally
    // Disconnect and free up memory
    httpconnect.Disconnect;
    httpconnect.DisposeOf;
  end;
end;

Why is there no memory release for the postdata? How correctly on pascal?
How correctly should this code look on pascal?
Thanks.
Lena
BCBJ Master
BCBJ Master
 
Posts: 482
Joined: Sun Feb 06, 2011 1:28 pm

Re: Memory release on pascal

Postby rlebeau » Mon Apr 10, 2017 5:49 pm

Lena wrote:I need to use the *.pas file in my program. I think there's an mistake In this file.


There is not, actually.

Lena wrote:Why is there no memory release for the postdata?


The code is clearly written for mobile platforms only. Both the iOS and Android compilers use ARC for memory management of objects, meaning the objects are reference counted by the compiler and will automatically be freed when their reference counts fall to 0. The use of DisposeOf() in this example is redundant since the http object goes out of scope immediately after being manually disposed of, so it would have been freed anyway.

Lena wrote:How correctly should this code look on pascal?


If you are really paranoid about it (or, if you need to use the same code on non-ARC platforms - Windows or OSX), then it should look like this:

Code: Select all
procedure RegisterDevice(DeviceID : string; DeviceToken : string);
var
  // Data to be sent to the script
  postdata: TStringList;
  //Data connection
  httpconnect : TIdHTTP;
begin
  // Create a connection
  httpconnect := TIdHTTP.Create;
  try
    // Specify the data to send
    postdata := TStringList.Create;
    try
      postdata.Add('action=register-device');
      postdata.Add('did=' + DeviceID);
      postdata.Add('token=' + DeviceToken);
      {$ifdef ANDROID}
      postdata.Add('platform=android');
      {$else}
      postdata.Add('platform=ios');
      {$endif}

      //send
      httpconnect.Post(DOMAIN + 'push.php', postdata);
    finally
      postdata.Free;
    end;
  finally
    httpconnect.Free;
  end;
end;
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1415
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Memory release on pascal

Postby Lena » Tue Apr 11, 2017 12:15 am

Thank you very much. Please add an Exception in Pascal if this method
httpconnect.Post(DOMAIN + 'push.php', postdata);
will generate an exception.
Lena
BCBJ Master
BCBJ Master
 
Posts: 482
Joined: Sun Feb 06, 2011 1:28 pm

Re: Memory release on pascal

Postby rlebeau » Tue Apr 11, 2017 9:31 am

Lena wrote:Thank you very much. Please add an Exception in Pascal if this method
httpconnect.Post(DOMAIN + 'push.php', postdata);
will generate an exception.


Code: Select all
procedure RegisterDevice(DeviceID : string; DeviceToken : string);
var
  // Data to be sent to the script
  postdata: TStringList;
  //Data connection
  httpconnect : TIdHTTP;
begin
  try
    // Create a connection
    httpconnect := TIdHTTP.Create;
    try
      // Specify the data to send
      postdata := TStringList.Create;
      try
        postdata.Add('action=register-device');
        postdata.Add('did=' + DeviceID);
        postdata.Add('token=' + DeviceToken);
        {$ifdef ANDROID}
        postdata.Add('platform=android');
        {$else}
        postdata.Add('platform=ios');
        {$endif}

        //send
        httpconnect.Post(DOMAIN + 'push.php', postdata);
      finally
        postdata.Free;
      end;
    finally
      httpconnect.Free;
    end;
  except
    on E: Exception do begin
      // do something
    end;
  end;
end;
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1415
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA


Return to Technical

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 5 guests