Jump to content

zed

Members
  • Content Count

    24
  • Joined

  • Last visited

Posts posted by zed


  1. @vfbb How about using a lock? Try this fix:

    diff --git a/Unit1.pas b/Unit1.pas
    index e0d57a2..a27eef0 100644
    --- a/Unit1.pas
    +++ b/Unit1.pas
    @@ -6,7 +6,7 @@ interface
     uses
       System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
       FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Threading,
    -  FMX.Controls.Presentation, FMX.StdCtrls;
    +  FMX.Controls.Presentation, FMX.StdCtrls, System.SyncObjs;
     
     type
       ITest = interface
    @@ -38,6 +38,7 @@ type
         FThreadsPool: TArray<TThreadPool>;
         FTest: ITest;
         FTestWeak: TArray<TWeakOfTest>;
    +    FLock: TCriticalSection;
       public
         { Public declarations }
       end;
    @@ -54,6 +55,7 @@ implementation
     
     procedure TForm1.FormCreate(Sender: TObject);
     begin
    +  FLock := TCriticalSection.Create;
       FTestControlThreadPool := TThreadPool.Create;
       FTestControlTask := TTask.Run(
         procedure()
    @@ -85,17 +87,32 @@ begin
               begin
                 while (not FDestroying) and (not FCanceled) do
                 begin
    -              LTest := Form1.FTestWeak[(Length(Form1.FTestWeak) div 2)-1].FTestWeak; // Use a weak reference that is in the middle of the list of weak references
    +              FLock.Acquire;
    +              try
    +                LTest := Form1.FTestWeak[(Length(Form1.FTestWeak) div 2)-1].FTestWeak; // Use a weak reference that is in the middle of the list of weak references
    +              finally
    +                FLock.Release;
    +              end;
                   if Assigned(LTest) then // Test if the strong reference is valid, that is, theoretically verify that the object has not yet been destroyed
                   begin
                     LTest.Test; // Just a useless call, because if the reference still exists and the object has been destroyed, this will give an exception
    -                LTest := nil; // Removing the strong reference
    +                FLock.Acquire;
    +                try
    +                  LTest := nil; // Removing the strong reference
    +                finally
    +                  FLock.Release;
    +                end;
                   end;
                 end;
               end, Form1.FThreadsPool[I]);
             end;
             Sleep((Random(20) + 3) * 500); // Wait all tasks run many AddRef / Release simulating a stress test
    -        Form1.FTest := nil; // Remove the strong ref to force the object destruction (Note: we don't know what task will destroy, will be the last that remove the strong ref)
    +        FLock.Acquire;
    +        try
    +          Form1.FTest := nil; // Remove the strong ref to force the object destruction (Note: we don't know what task will destroy, will be the last that remove the strong ref)
    +        finally
    +          FLock.Release;
    +        end;
             Sleep((Random(10) + 1) * 200); // Wait some seconds to ensure the object destruction
     
             // Cancel the test, check if have error, free all objects and get ready to repeat the next test loop
    @@ -137,6 +154,7 @@ begin
       FTestControlTask.Wait;
       FTestControlTask := nil;
       FTestControlThreadPool.Free;
    +  FLock.Free;
     end;
     
     { TTest }

    To protect your data you need to use a lock every time you assign value to the LTest and Form1.FTest.


  2. 3 hours ago, zed said:

    License extension is broken at the moment. I have reported the problem to Embarcadero (thanks Cathy Hundley), so it remains to wait while they fix the bug.

     

    By the way, exactly the same problem with license extending was a year ago...

    @Oberon82 I have just been informed that the problem has been fixed and you can request the key as usual: https://www.embarcadero.com/products/delphi/starter/free-download/

    • Like 2
×