Jump to content

Lars Fosdal

Administrators
  • Content Count

    3335
  • Joined

  • Last visited

  • Days Won

    110

Posts posted by Lars Fosdal


  1. A workaround could be to explicitly allocate system memory outside of the regular memory manager, but that would require using specific methods for each platform.

    1 hour ago, DelphiUdIT said:

    EmptyWorkingSet(GetCurrentProcess)

    How does that work with memory allocations in FastMM?


  2. The while true causes an infinte loop, which doesn't do anything but looping and sleeping after the first run, so why this can't run for days and days witout running out of memory, is a bit of a mystery.

    How do you monitor the memory usage?

     

    Building these fairly large strings will have the MemoryManager allocate memory blocks which will remain allocated (and reusable) after you free your classes. These will be allocated from system memory until the application exits. 

    In some corner cases, you can have memory fragmentation, which is caused by the elements in the list of released blocks of memory, being to small to be reused to hold the new objects, but in you example, that is not the issue.

     

    The line

    TListaString(V_Listas[0]).V_String := '';

    is not necessary, as the string type is reference counted and automatically will be released.

    Apart from that,  I guess there is a point to why you are NOT using TStringList, but using other containers and methods in classes to encapsulate reused code, could greatly reduce the amount of code duplication - which again reduces the potential for errors.

    See System.Contnrs TObjectList and System.Generics.Collections TObjectList for examples of lists that can manage your objects.

     

    Tip - For the purpose of testing, you would want each time you test to be identical - hence setting RandSeed to a fixed value at the start will ensure you have repeatable execution.

     


  3. 56 minutes ago, Uwe Raabe said:
    1 hour ago, PeterPanettone said:

    Please VOTE & SHARE.

    The new QP doesn't support voting and I cannot see a benefit in sharing this.

    Well, sharing allows discovery, and those that are affected can add a comment?

    • Like 1

    • Which language
    • Which version
    • Which class is SslHttpRest, and  from which unit?

    If you use System.Net.HttpClient (which I use, because getting the REST classes to work with various flavours of OAuth2 turned out to be a challenge), you can do it something like this (code is not complete 😞

    var
      HTTPRequest: IHttpRequest; // from System.Net.HttpClient
      HttpResponse: IHttpResponse; // System.Net.HttpClient
      ReqHeaders: TNetHeaders; // System.Net.URLClient
      URL: String;
    begin
      try
        try
          try
            HTTPRequest := HTTP.GetRequest('GET', URL);
    
            ReqHeaders := [
              TNetHeader.Create('Content-Type', 'application/json'),
              TNetHeader.Create('Content-Length, ''),
              TNetHeader.Create('Store-Token', '22345673301244567896663456789012'),
              TNetHeader.Create('User-Agent', 'PostmanRuntime/7.37.3'),
              TNetHeader.Create('Accept', '*/*'),
              TNetHeader.Create('Postman-Token', '40f28212-2f71-487a-a22f-d6ecdfa61b8b'),
              TNetHeader.Create('Host', 'localhost:8080'),
              TNetHeader.Create('Accept-Encoding', 'gzip, deflate, br'),
              TNetHeader.Create('Connection', 'keep-alive')
            ];
    
            HttpResponse := HTTP.Execute(HTTPRequest,nil, ReqHeaders); // HTTP is THTTPClient from System.Net.HttpClient
    ...

     


  4. Can we stay on the topic, please?

    Are there any practical languages that are applicable to writing the same variety of solutions as Delphi, that are actually memory safe?

    Even if you manage your memory in Delphi, it is not hard to get corrupted data due to a dangling or misdirected pointer, even in Delphi.

    • Like 2

  5. On 4/13/2024 at 11:24 PM, Anders Melander said:

    Maybe you should think a bit more about that. Ideally until it is no longer a mystery.

    IMO, that was not very helpful.
    If you are a new user, you may need to be guided on how to do it right, not just be told that you are doing it wrong.

    • Like 1

  6. What if you treat every value as a generic type?
    That way you are free to use TypeInfo?

    procedure TParam<T>.SetAsString(const aValue: string);
    var
      TV: TValue;
    begin
      TV := TValue.From<T>(Default(T));
      try
        case TV.Kind of
          tkEnumeration: TV := TValue.FromOrdinal(TypeInfo(T), GetEnumValue(TypeInfo(T), aValue));
          tkInteger: TV := TValue.From<Integer>(StrToInt(aValue));
          tkInt64:   TV := TValue.From<Int64>(StrToInt(aValue));
          tkFloat:   TV := TValue.From<Extended>(StrToFloat(aValue));
                else TV := TValue.From<String>(aValue);
        end;
        FValue := TV.AsType<T>;
      except
        on E:Exception
        do begin
          CmdDebugOut(Parent.Debug + ': "' + aValue + '" -> ' + E.Message);
          FValue := Default(T);
        end;
      end;
    end;
    
    function TParam<T>.GetAsString: string;
    var
      TV: TValue;
    begin
      TV := TValue.From<T>(FValue);
      Result := TV.AsString;
    end;

     


  7. I should simply stop posting without actually checking shit...

     

      object Label1: TLabel
        Left = 303
        Top = 233
        Width = 99
        Height = 15
        Caption = 'Text'#32'Text'
      end

     

    does however, work correctly.

    Edit
    But if you actually edited the dfm text and put in that #32, then view as form, and back to view as text - it has been converted to 'Text Text'.

    As @Lajos Juhászpoints out - Unicode text that is entered in the form attribute editor, will translate to #1234 values.

    • Like 1

  8. Ref := ItemProvider.Grab(ItemId); 
    // or Ref := ItemProvider.GrabForChange(ItemId);
    try
      // do stuff
    finally
      ItemProvider.Drop(Ref);
    end;

    ItemProvider can do the allocation and loading, as well as the disposal.
    If there is parallell use, it can secure against readers/other writers, have a keep-alive in cache period, etc.

    In theory, with the "smart pointer" trick, you could even do away with the try/finally.

    • Like 1
×