REST request memory leak when no character is returned in the header

Using Delphi 10.2 (Tokyo)

Below is the code for a full console application that displays an unexpected memory leak (TUTF8Encoding) when calling one URL and memory leak when calling another.

Comparing headers between two answers:

What a memory leak contains

Content-Type=application/json 

One that contains no memory leak contains

  Content-Type=application/json; charset=utf-8 

Is this a mistake, or should I do something to prevent this?

 program RESTMemLeakTest; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, IPPeerClient, REST.Client, REST.Types; var RESTClient1: TRESTClient; RESTRequest1: TRESTRequest; URL: string; begin ReportMemoryLeaksOnShutdown := True; URL := 'https://httpbin.org/post'; // memory leak //URL := 'https://jsonplaceholder.typicode.com/posts'; // no memory leak RESTClient1 := TRESTClient.Create(URL); RESTRequest1 := TRESTRequest.Create(nil); try try RESTRequest1.Client := RESTClient1; RESTRequest1.Method := rmPOST; RESTRequest1.Execute; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; finally RESTRequest1.Free; RESTClient1.Free; end; end. 

Running the application with a URL that memory leak returns this:

An unexpected memory leak has occurred. Unexpected small leak block:

21 - 28 bytes: TUTF8Encoding x 1

Update . Setting the FallbackCharsetEncoding parameter to an empty string means to fix a memory leak. No known problems (yet) this is being done. I am going to open an error report with Embarcadero to find out what they are saying. Therefore, adding the line below before executing the query will prevent an unexpected memory error message.

 RESTClient1.FallbackCharsetEncoding := ''; 

Update 2 : The RSP-17695 error report was sent on March 30, 2017.

Update 3 : August 8, 2017: bug resolved in version 10.2 of Tokyo Release 1

+5
source share
1 answer

To avoid memory leak, there is a possible workaround for this:

 RestClient.FallbackCharsetEncoding := ''; 

By setting the backup encoding to an empty or raw string, the leak of the code branch from the REST library will not be executed, and therefore you will not receive the leak of an unreleased TEncoding instance (which is acquired using GetEncoding ()).

But this, of course, is only wokrs if you are fine using a backup of the source code.

This works in an update in Berlin 2. It can probably work in later versions before the patch in Tokyo Update 1.

0
source

Source: https://habr.com/ru/post/1266081/


All Articles