Simple Object recycling framework for Delphi

  Origin 1 

  I have a 1.1 wrong procedures 

  Program name: call processing module pressure test tools, divided into client and server. 

  Development Tools: Delhpi 5 

  Related technologies: the client and server through the establishment of Socket connected to simulate a group of incoming phone, keys, wait, hang up, and so the process.    Socket server on the incident and received data packets pretreatment, and call into abstract model data, and then sent to more senior call-processing module.    The call-processing module is hardware-independent (and voice boards, switches types are unrelated), the passage of the pressure testing tools can be more true simulation massive call to achieve the call processing module testing procedures and correctness of the logic of performance. 

  The system design of certain considerations, the testing tools are divided into client and server to achieve two procedures, and using socket communications.    Now want, in fact better integrated into a program to achieve a more simple - but also because the two procedures used to achieve, touched off some of the problems behind, and that the introduction of a simple garbage collection framework. 

  1.2 problems 

  In the process of testing the use of tools, we found that when the huge volume of calls, and test tools frequent moves circumstances, the system in the following error: 

  N visit to the wrong address (EAccessViolation), the code is located near the $ 0046 FC80 visit to address more than $ 00000028. 

  N EinvalidCast a mistake, the mistake of an address for that category type conversion error (used as keywords). 

  N procedures asserted multiple failures, cited a number of targets have been destroyed. 

  After careful inspection procedures, I still think that all this is simply incredible!    Moreover, other procedures have been used to test the procedures of this type has its own problems, I almost shame! 

  In order to save his own reputation, I have to a Chenzhuqi to carefully tracking errors, to resolve the problem! 

  2 solution 

  2.1 Zhaxi 

  In fact, the solution to the problem rather smoothly. 

  Show through the call stack procedures, procedures found errors before Socket always remain in the data packets sent in the process.    Then, single-step further by tracking found in the data packets sent in the process of detection to the Socket-connection has been disconnected, it will trigger OnDisconnect incident.    And I was ServerSocket OnDisconnect the incident in accordance with the transfer incoming Socket handle, find the corresponding objects will be destroyed. 

  I ServerSocket OnDisconnect incident in the code are as follows: 

  Procedure Txxxx.ServerClientDisconnect (Sender: TObject; 

  Socket: TCustomWinSocket); 

Begin

…

  FLines.DestroyLineBySocket (Socket); / / This one is in the timing of the release of unsuitable objects 

…

End;

  The question is so arise. 

  For example, in a process with the following code (the front line): 

  1 FLine.DoSomething; 

  2 FLine.SendSocketData; 

  3 FLine.DoOtherThings; 

  Among them, all the way FLine call on behalf of the object.    The object quoted an internal TCustomWinSocket pointer.    SendSocketData Socket is to use this data sent. 

  Flines TLine object is an example of such containers. 

  It is thus interpreted these various errors: 

  1.    As Line 2 of the link disconnect lead FLine Socket object release, it will almost certainly be DoOtherThings 3 visit to a wrong address; 

  2.    Line 2 as the target of destruction, procedures similar to the "Object as TLine" code led to the second category mistake; 

  3.    As early targets destroyed, rehabilitation work has not led to the third category the wrong place; 

  2.2 solutions 

  Understand his reasons, the problem can be easily solved with more. 

  There are only two problems mentioned above programme: 

  First, determine whether there examples 

  In DoOtherThings, FLine determine whether the remains Flines, if it continues to deal with, otherwise the end of treatment; 

  Second, delay the destruction of objects FLine 

  In the OnDisconnect ServerSocket, will be dumped into Lajiche FLine object, and when the time is ripe to be destroyed. 

  A programme to consider changes to the code of larger At the same time, such programmes are not very beautiful code, it decided to adopt the two programmes, namely the introduction of garbage collection mechanism to solve the problem.    Option 2 points is a suitable choice of the timing of the destruction of the real object.    For this, the problem倒不, simply select news cycle in the section dealing with information can be a part of recovery.    Because after dealing with the sessions, to ensure that the inevitable FLine whether it is still effective inspection. 

  3 Simple Object recycling Framework (untGarbagCollector) 

  3.1 Summary 

  Summary of recycling is simple: 

  N TThreadList support the use of concurrent threads visit to recovery and the preservation of the object pointer; 

  N Put provide recycling methods to be preserved; 

  N Recycle methods provide a genuine recovery (because all objects are derived from the TObject). 

  3.2 codes 

  Unit untGarbagCollector; 

  Interface 

  Uses 

Classes;

  Type 

  TGarbagCollector = Class (TObject) 

  Private 

  FList: TThreadList; 

  Public 

  Constructor Create; 

  Destructor Destroy; override; 

  Procedure Put (const AObject: TObject); 

  Procedure Recycle (const MaxCount: Integer); 

  End; 

  Function GarbagCollector: TGarbagCollector; 

  Implementation 

  Var 

  _GarbagCollector: TGarbagCollector; 

  Function GarbagCollector: TGarbagCollector; 

  Begin 

  If not Assigned (_GarbagCollector) then 

  _GarbagCollector: = TGarbagCollector.Create; 

  Result: = _GarbagCollector; 

  End; 

  () TGarbagCollect 

  Constructor TGarbagCollector.Create; 

  Begin 

  FList: = TThreadList.Create; 

  End; 

  Destructor TGarbagCollector.Destroy; 

  Begin 

  Try 

  Recycle (FList.LockList.Count); 

  Finally 

FList.UnlockList;

  End; 

FList.Free;

  End; 

  Procedure TGarbagCollector.Put (const AObject: TObject); 

  Begin 

  Try 

  FList.LockList.Add (AObject); 

  Finally 

FList.UnlockList;

  End; 

  End; 

  Procedure TGarbagCollector.Recycle (const MaxCount: Integer); 

  Var 

  I: Integer; 

  AList: TList; 

  Begin 

  AList: = FList.LockList; 

  Try 

  I: = 0; 

  While (AList.Count> 0) and (I <MaxCount) do 

  Begin 

  TObject (AList.Last). Free; 

  AList.Delete (AList.Count - 1); 

  Inc (I); 

  End; 

  Finally 

FList.UnlockList;

  End; 

  End; 

  Initialization 

  Finalization 

  If Assigned (_GarbagCollector) then 

_GarbagCollector.Free;

  End. 

  For example the use of 3.3 

  After quoted untGarbagCollector unit can be used directly GarbagCollector for destruction and recycling targets. 

  N destroy 

  AObject: = TObject.Create; 

  GarbagCollector.Put (AObject); 

  N Recovery 

  In the timer, threads, as well as other occasions call Recycle methods. 

  MaxCount is used to control the number of parameters for each destroyed, mainly because they are afraid that too many one-time occupation of the destruction of excessive cpu. 

  (Suddenly discovered that the limit can be extended to the destruction of time, for example, each time not more than the destruction of the n milliseconds). 

  3.4 Use occasions 

  In this case, in order to prevent the premature destruction caused visit object conflict, and the introduction of a recycling technology. 

  On other occasions, such as certain procedures in order to improve the subjective performance, but also be able to introduce the technology.    For example, the completion of certain tasks procedures in the treatment process will be the subject of the provisional, and the destruction of these objects are more time-consuming.    Therefore, in order to end as soon as possible and tasks, which were preserved to Lajiche temporary objects in the.    Question operations (task) complete, and after a period of time, such as cpu idle comparison, then the real destruction of temporary objects.    Is the essence of this approach to space for time - with certain pre-established system object, and re-use targets to improve performance of the same. 

  [The end] 

Tags: , , ,

Releated Articles


0 Comments to “Simple Object recycling framework for Delphi”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.