Simple Object garbage collection framework for Delphi

Source: Internet
Author: User

(i). Origin


1.1 One of my error programs

Program Name: Pressure test tool for Call processing module, divided into client and server.

Development tools: DELHPI 5

Related technologies: The client establishes a socket connection with the server to simulate the dialing, pressing, waiting, and hanging of a set of telephones. The server pre-processes the socket events and received packets and translates them into abstract call model data, which is then sent to the higher-level call processing module. Since the call processing module is hardware-independent (regardless of the voice board, switch type), this stress test tool can realistically simulate mass calls to achieve the purpose of testing the logic correctness of the call Processing module program and its performance.

Due to some considerations in the design of the system, the test tool is divided into two programs for the client and server, and the socket is used for communication. Now, it's not as simple as integrating into a single program-but also because it's implemented in two programs, it raises some of the problems behind it and introduces a simple garbage collection framework.


1.2 Questions

During the use of the test tool, we found that when the number of calls is large and the test tool is frequently active, the system has the following error:

Access address error (eaccessviolation), code address near $0046fc80, access address is more than $00000028.

A einvalidcast error occurred indicating that an error occurred during class-type conversion of an address (with the AS keyword).

Multiple assertions fail within the program, and many instances of objects that have been destroyed are present.

After examining the procedure carefully, I still think it is incredible! Moreover, the program that was used to test the other program itself had such problems, almost embarrassing me!

In order to redeem their reputation, I have to be careful to follow the mistakes, troubleshooting problems!


(ii). Solutions

2.1 Error Checking

In fact, the solution of the problem is still relatively smooth.

By looking at the program's call stack, we find that the program always stays in the process of sending the socket packet before it goes wrong. Then, further through the single-step tracking, found in the process of sending packets, the socket detects that the peer connection has been disconnected, it will trigger the OnDisconnect event. It is in the ServerSocket OnDisconnect event that I find the corresponding object to destroy according to the socket handle passed in.

My code in ServerSocket's OnDisconnect event is as follows:

Procedure Txxxx.serverclientdisconnect (Sender:tobject; Socket:tcustomwinsocket);
Begin
...
It is this sentence that releases the object at an inappropriate time.
Flines.destroylinebysocket (Socket);
...
End

The problem arises.

For example, you have the following code in a procedure (line number preceded):

1 fline.dosomething;
2 Fline.sendsocketdata;
3 fline.dootherthings;

Where Fline is the object that represents the call. An tcustomwinsocket pointer is referenced inside the object. Sendsocketdata is the use of this socket for data transmission.

Flines is an instance of the container class for the Tline object.

It is not difficult to read the aforementioned types of errors:

1. Because the socket connection of line 2 disconnects causing the Fline object to be released, row 3 access dootherthings almost inevitably results in an access address error;

2. Because the object of row 2 is destroyed, the code similar to "object as Tline" in the program results in a second type of error;

3. Due to the early destruction of the object, the remedial work is not in place resulting in a third type of error;


2.2 Solutions

Knowing the reason, the problem is much easier to solve. The above questions are two solutions:

To determine if an instance exists

After dootherthings, determine whether the Fline object is still in the flines, if it continues to deal with, or end processing;

Second, delay destroying the Fline object

In ServerSocket's OnDisconnect, the Fline object is thrown into the garbage pool and destroyed when the time is ripe.

In view of the large amount of code to be changed by the scheme, and the less graceful code of this scheme, it is decided to adopt scenario two, which is to introduce garbage collection mechanism to solve the problem. The point of programme two is to choose the right time to actually destroy the object. For this, the problem is not very large, just select the message loop to process the first link of the message to be recycled. Because in the subsequent processing process, it is inevitable to ensure that the fline is still effective check.

(iii). Simple Object garbage Collection framework (Untgarbagcollector)
3.1 Overview

Easy garbage Collection is simple:

Use Tthreadlist to support concurrent access of threads and to save object pointers for collection;

Provide the Put method to save the object to be recycled;

Provides a recycle method for true recycling (since all objects derive from TObject).

3.2 Implementation Code

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.

3.3 Examples of Use

After referencing the Untgarbagcollector unit, you can use Garbagcollector to destroy and reclaim objects directly.

Destruction
Aobject: = tobject.create;
Garbagcollector.put (Aobject);

Recycling
The Recycle method can be invoked on timers, threads, and other occasions.

Maxcount is used to control the number of each destruction of the parameters, mainly afraid of destroying too much CPU at once.

(sudden discovery can also be extended to limit time for destruction, such as n milliseconds per destruction).


3.4 Use Cases

In this case, the garbage collection technique was introduced in order to prevent the object from being prematurely destroyed by an access violation.

In other situations, such as in order to improve the subjective performance of some programs, you can also introduce the technology. For example, the completion of certain tasks of the program, the process will produce temporary objects, and the destruction of these objects is more time-consuming. Therefore, these temporary objects can be saved to the garbage pool in order to end the task as soon as possible. Wait for the job (Task) to complete, and after a period of time after the CPU is more idle, then the temporary object is really destroyed. The essence of this approach is to exchange space for time-the same way that some systems pre-create objects and reuse objects to improve performance.

Http://www.lsworks.net/article/91.html

Simple Object garbage collection framework for Delphi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.