Jump to content

Alberto Paganini

Members
  • Content Count

    33
  • Joined

  • Last visited

Community Reputation

3 Neutral

About Alberto Paganini

  • Birthday May 25

Technical Information

  • Delphi-Version
    Delphi XE2

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. Alberto Paganini

    Delete FastMM4 MemoryManager_EventLog.txt

    Thank you David, this did the trick !
  2. Alberto Paganini

    Delete FastMM4 MemoryManager_EventLog.txt

    I did and created this test: program Project3; {$APPTYPE CONSOLE} {$R *.res} uses FastMM4, system.Classes, System.SysUtils; var t:tstringlist; begin t:=TStringList.Create; end. I ran it a number of times and the new reports are appended to the old ones. Obviously there must be something wrong in my FastMM4 settings. I will look into that.
  3. I use FastMM4 to check memory leaks and I noticed the report file " MyApplicatoin+MemoryManager_EventLog.txt" is not deleted automatically once I have amended the source and I run my application again. This way if there are still memory leaks they are added to the existing report and this confuses me. Is there an option in FastMM4 to achieve that I am not aware of or do I have to rely on DeleteFile(...) when I run the application ? Many thanks Alberto
  4. Alberto Paganini

    Varialbe set to nil

    @Stefan Glienke I have amended the example according to your advice. program Project3; {$APPTYPE CONSOLE} {$R *.res} uses FastMM4, system.Classes, System.SysUtils, Spring; type MyInterface2 = interface ['{8E7BC71F-55EE-4345-8A5B-42C433BE9EBA}'] procedure print; end; MyInterface1 = interface ['{607A66A6-D68C-4980-9FB1-83B325EE9A91}'] procedure SetInterface2(const aInterface: MyInterface2); procedure DoSomethingWithInterface2; end; TMyClass1 = class(TInterfacedObject, MyInterface1) private //RefToInterFace2: Pointer{MyInterface2}; RefToInterFace2: Weak<MyInterface2>; public procedure SetInterface2(const aInterface: MyInterface2); procedure DoSomethingWithInterface2; end; TMyClass2 = class(TInterfacedObject, MyInterface2) private // Ref: MyInterface1; ref: Weak<MyInterface1>; public constructor Create(int: MyInterface1); reintroduce; destructor Destroy; override; // this can be deleted procedure print; end; { TMyClass1 } procedure TMyClass2.print; begin Writeln('TMyClass2.print'); Sleep(1500); end; { TMyClass2 } procedure TMyClass1.DoSomethingWithInterface2; begin if RefToInterFace2 <> nil then begin // MyInterface2(RefToInterFace2).print; RefToInterFace2.Target.print; end; end; procedure TMyClass1.SetInterface2(const aInterface: MyInterface2); begin // RefToInterFace2 := Pointer(aInterface); RefToInterFace2.Target := aInterface; end; constructor TMyClass2.Create(int: MyInterface1); begin inherited Create; if int <> nil then begin Ref.Create(int); Ref.Target.SetInterface2(Self); end; end; destructor TMyClass2.Destroy; begin // if Ref <> nil then // MyInterface1(Ref).SetInterface2(nil); inherited Destroy; end; var aMyClass1: MyInterface1; aMyClass2: MyInterface2; begin aMyClass1 := TMyClass1.Create; aMyClass2 := TMyClass2.Create(aMyClass1); aMyClass1.DoSomethingWithInterface2; end. Is that acceptable? In which case the pointer approach proposed by Remi may cause AV errors? Many thanks Alberto
  5. Alberto Paganini

    Varialbe set to nil

    Hi Remi, I had to amend your code with the following one in order to make the example compile by DX2 procedure TMyClass1.SetInterface2(const aInterface: MyInterface2); begin RefToInterFace2 := Pointer(aInterface); end; The rest is all spot on! I suppose that the advantage to use the second solution is you can call any method of MyInterface1 throughout TMyClass2 by using Ref while in the first one you can do it only in TMyClass2.Create as MyInterface1 is not stored in a variable. Thank you very much for taking the time. Alberto
  6. Alberto Paganini

    Varialbe set to nil

    But If I store the interface in a local variable in TMyClass2, Ref in my example, I have a leak instead. It seems the RTL cannot have access to it. The only way to release the variable is to use a method and call this method from the main begin..end. Here below the minimum example. program Project3; {$APPTYPE CONSOLE} {$R *.res} uses FastMM4, system.Classes, System.SysUtils; type MyInterface2 = interface ['{8E7BC71F-55EE-4345-8A5B-42C433BE9EBA}'] procedure print; end; MyInterface1 = interface ['{607A66A6-D68C-4980-9FB1-83B325EE9A91}'] procedure SetInterface2(aInterface: MyInterface2); procedure DoSomethingWithInterface2; end; TMyClass1 = class(TInterfacedObject, MyInterface1) private RefToInterFace2: MyInterface2; public procedure SetInterface2(aInterface: MyInterface2); procedure DoSomethingWithInterface2; destructor Destroy;override; end; TMyClass2 = class(TInterfacedObject, MyInterface2) private Ref: MyInterface1; public constructor Create(int: MyInterface1); reintroduce; procedure print; end; { TMyClass1 } procedure TMyClass2.print; begin Writeln('TMyClass2.print'); Sleep(1500); end; { TMyClass2 } destructor TMyClass1.Destroy; begin SetInterface2(nil);// this isn not executed inherited; end; procedure TMyClass1.DoSomethingWithInterface2; begin RefToInterFace2.print; end; procedure TMyClass1.SetInterface2(aInterface: MyInterface2); begin RefToInterFace2 := aInterface; end; constructor TMyClass2.Create(int: MyInterface1); begin inherited Create; Ref := int; Ref.SetInterface2(Self); end; var aMyClass1: MyInterface1; aMyClass2: MyInterface2; begin aMyClass1 := TMyClass1.Create; aMyClass2 := TMyClass2.Create(aMyClass1); aMyClass1.DoSomethingWithInterface2; aMyClass1.SetInterface2(nil);// this releases the variable end. Is this the only way to release the pointer in the variable or is there a better way? If I add a destructor that is not executed. Many thanks Alberto
  7. Alberto Paganini

    Varialbe set to nil

    I am just investigating on how this works. Having the following code: program Project3; {$APPTYPE CONSOLE} {$R *.res} uses FastMM4, system.Classes, System.SysUtils; type MyInterface2 = interface ['{8E7BC71F-55EE-4345-8A5B-42C433BE9EBA}'] procedure print; end; MyInterface1 = interface ['{607A66A6-D68C-4980-9FB1-83B325EE9A91}'] procedure SetInterface2(aInterface: MyInterface2); end; TMyClass1 = class(TInterfacedObject, MyInterface1) private RefToInterFace2: MyInterface2; public procedure SetInterface2(aInterface: MyInterface2); end; TMyClass2 = class(TInterfacedObject, MyInterface2) constructor Create(int: MyInterface1); reintroduce; procedure print; end; { TMyClass1 } procedure TMyClass2.print; begin Writeln('TMyClass2.print'); Sleep(1500); end; { TMyClass2 } procedure TMyClass1.SetInterface2(aInterface: MyInterface2); begin RefToInterFace2 := aInterface; RefToInterFace2.print; end; constructor TMyClass2.Create(int: MyInterface1); begin inherited Create; int.SetInterface2(Self); end; var aMyClass1: MyInterface1; aMyClass2: MyInterface2; begin aMyClass1 := TMyClass1.Create; aMyClass2 := TMyClass2.Create(aMyClass1); end. How does TMyClass1 know there is RefToInterFace2 to be set to nil in order to prevent an AV error? Where is RefToInterFace2:=nil invoked from? I mean, TMyClass1 ancestor does not know about RefToInterFace2 and RefToInterFace2 is not set to nil anywhere in TMyClass1 Many thanks Alberto
  8. Alberto Paganini

    One more memory leak and FastMM4

    Sure, here it is in attach. I just wanted to avoid to attach a minimal example every time. Test3.zip
  9. Alberto Paganini

    One more memory leak and FastMM4

    Ok, I have also implemented QueryInterface, AfterConstruction, BeforeDestruction and NewInstance in my DataModule In this case, there are no leaks in both cases, with our without .AsSingleton. At this stage, I started implementing the other classes, one by one and .AsSingleton in my datamodule started making a difference when I arrived at the class TBetFairAPINGHorseFunctionSimulationBL TBetFairAPINGHorseFunctionSimulationBL = class(TInterfacedObject, ILBS_BetfairAPINGFunctionsBL) private FDM: IMainDMTEST; public constructor Create(aDM: IMainDMTEST); reintroduce; destructor Destroy; override; end; constructor TBetFairAPINGHorseFunctionSimulationBL.Create(aDM: IMainDMTEST); begin inherited create; FDM := aDM; end; destructor TBetFairAPINGHorseFunctionSimulationBL.Destroy; begin FDM := nil; inherited; end; I have added TBetFairAPINGHorseFunctionSimulationBL.Destroy and set FDM=nil in there but this destructor is never executed, don't know why as this class inherits from TInterfacedObject? As the last attempt, I have added a line at the end of the program to try to release the IMainDMTest at the very end of the application but the leak is still there. var FScalpingBL: IScalpingBL; Test: IMainDMTEST; begin RegisterTypes(GlobalContainer); Test := GlobalContainer.Resolve<IMainDMTEST>; FScalpingBL := globalcontainer.Resolve<IScalpingBL>; Test := nil; end. If I remove .AsSingleton when I resolve the interface then I have no more leaks but I have to keep .AsSingleton because this class holds/manages data and several classes have access to it.
  10. Alberto Paganini

    One more memory leak and FastMM4

    Sorry I didn't explain properly. _AddRef and _Release are implemented in my example. It was the other procedures from Jeroen's example that have been left out as you advised. If I create the object as per your example above then there is no leak. I suppose the issue is in the way I resolve the interface in the Spring container.
  11. Alberto Paganini

    One more memory leak and FastMM4

    I looked into TInterfacedObject again and implemented _AddRef and _Release in the same way, leaving out the remaining procedures, the example attached to this post is the updated minimum example. Unfortunately, I still have the same leaks. I started that thread. Now I know why I cannot have access to it any longer. Test2.zip
  12. Alberto Paganini

    One more memory leak and FastMM4

    I have managed to make a minimal example that leaks. I will attach it here. I have found this article on how to make a TDataModule reference counting written by Jeroen Pluimers https://wiert.me/2009/08/10/delphi-using-fastmm4-part-2-tdatamodule-descendants-exposing-interfaces-or-the-introduction-of-a-tinterfaceddatamodule/ and tried to implement it in MainDMSimulation in the example. Despite that, the example is still leaking. I think is I am still doing something wrong when it comes to making TDataModule reference counting or in the way I resolve the interface in the Spring4D container (or maybe both). Test.zip
  13. Alberto Paganini

    One more memory leak and FastMM4

    TBetFairApi.GetToken works perfectly now, thank you very much! Now I have to debug the rest because, you are right, there is another leak elsewhere. I will work on a minimal example. Thank you again, everybody. I have learnt a few new things !
  14. Alberto Paganini

    One more memory leak and FastMM4

    I did more investigation on the leaking instruction and found out that LJSONValue.Value; is UnicodeString. As FToken was a string I changed FToken into a UnicodeString but this didn't change anything. I don't know if this can help.
  15. Alberto Paganini

    One more memory leak and FastMM4

    Hi Remy, I tried the code you suggests but I still have leaks. The following is the report: --------------------------------2020/6/24 16:32:52-------------------------------- A memory block has been leaked. The size is: 116 This block was allocated by thread 0x2D9C, and the stack trace (return addresses) at the time was: 404DA6 [System.pas][System][@GetMem$qqri][3454] 408BBB [System.pas][System][@NewUnicodeString$qqri][19030] 408DEC [System.pas][System][@UStrFromPWCharLen$qqrr20System.UnicodeStringpbi][19697] 40A0C9 [System.pas][System][@UStrCopy$qqrx20System.UnicodeStringii][24873] E5CAD3 [Data.DBXPlatform.pas][Data.DBXPlatform][Dbxplatform.TDBXStringBuffer.ToString$qqrv][697] E8E8BC [Data.DBXJSON.pas][Data.DBXJSON][Dbxjson.TJSONString.Value$qqrv][1541] E92948 [uBetFair.pas][uBetFair][TBetFairApi.GetToken$qqrv][238] E92E00 [uTokenBL.pas][uTokenBL][TTokenBL.GetToken$qqrv][30] E5B593 [Main.pas][Main][TMainForm.FormActivate$qqrp14System.TObject][402] 557ED9 [Vcl.Forms][Forms.TCustomForm.Activate$qqrv] 558F99 [Vcl.Forms][Forms.TCustomForm.CMActivate$qqrr27Winapi.Messages.TWMNoParams] 4B4930 [Vcl.Controls.pas][Vcl.Controls][Controls.TControl.WndProc$qqrr24Winapi.Messages.TMessage][7204] 76EAC431 [Unknown function at gapfnScSendMessage] 76EAC3EE [Unknown function at gapfnScSendMessage] 76EAC380 [Unknown function at gapfnScSendMessage] The block is currently used for an object of class: UnicodeString The allocation number is: 60670 and the following is the line 238 FToken := LJSONValue.Value; I am confused here. If I don't have to free TJSONValue why is there this line in the code you propose? finally LJSONValue.Free; end; What am I missing here?
×