From: http://hi.baidu.com/marktian/blog/item/e7e060dec0b29d5fcdbf1a45.html
I. Why is stack clearance used?
Stack clearing is mainly used to handle memory that may be called abandoned or leaked when an exception exits.
See the following code:
Void unsafefunctionl ()
{
Cclanger * clanger = new (eleave) cclanger ();
Clanger-> initializel ();
........ // Skip
Delete clanger;
}
Analysis: if an exception occurs when clanger-> initializel () runs, the heap memory indicated by clanger will leak. This example is mentioned in one of the three steps.
So how can we deal with this potential error?
It is easy to think that we can use trapd to capture exceptions to release the heap memory. The above code will be modified as follows:
Void unsafefunctionl ()
{
Cclanger * clanger = new (eleave) cclanger ();
Trapd (error, clanger-> initializel ());
If (kerrnone! = Error)
{
Delete clanger;
}
........ // Skip
Delete clanger;
}
That is to say, capture exceptions through trapd and then judge the error code. If an exception occurs, call Delete to release the heap memory.
Of course, the above method is feasible. However, if there are multiple functions that may be abnormal, if we use trapd to capture exceptions, the system overhead will be very high, stack clearing exists to solve this problem, and it solves a series of such problems well.
Ii. Use clear Stack
The cleanupstack class is defined in the header file e32base. H. You can use the static member functions provided by this class to access the cleanup stack.
Stack cleanup principle:
Before calling code that may cause an abnormal exit, non-abnormal Exit security objects should be placed on the clear stack, which ensures that these objects can be correctly destroyed when an abnormal exit occurs. When an exception exits, the stack clearing function recycles all objects on the stack.
The following is an example of how to clear a stack:
Void safefunctionl ()
{
Cclanger * clanger = new (eleave) cclanger;
Cleanupstack: pushl (clanger );
Clanger-> initializel ();
Clanger-> dosomethingelsel ();
Cleanupstack: Pop (clanger );
Delete clanger;
}
In fact, the last two statements in this function
Cleanupstack: Pop (clanger );
Delete clanger;
You can use the cleanupstack: popanddestroy (clanger); statement to replace them. They are equivalent.
If an exception exits when clanger-> initializel (); or clanger-> dosomethingelsel (); is called, The clanger object will be cleared and destroyed by the stack.
Comments:Only when your call may (as long as possible) cause abnormal exit, you must clear the stack, otherwise (that is, you can fairly confirm that subsequent operations will not cause exceptions), there is no need, and if you do so, it is a waste of system resources, clear a pointer on the stack. Although it only contains 4 bytes, it is also meat!
Sequence problems:
Objects must be pushed in strict order and pushed out to clear stacks. A set of POP () operations must be in the opposite order of pushl () calls. The pop () or popanddestroy () pop-up objects may be called without naming them, but it is best to name the pop-up objects.
Example:
Void contrivedexamplel ()
{
Csiamese * sealpoint = newl (eseal );
Cleanupstack: pushl (sealpoint );
Csiamese * chocolatepoint = newl (echocolate );
Cleanupstack: pushl (chocolatepoint );
Csiamese * viotpoint = newl (eviolet );
Cleanupstack: pushl (viletpoint );
Csiamese * bluepoint = newl (eblue );
Cleanupstack: pushl (bluepoint );
Sealpoint-> catchmousel (); // The stack clearing statement is placed before the code that may exit unexpectedly.
Cleanupstack: popanddestroy (bluepoint );
Cleanupstack: popanddestroy (viletpoint );
Cleanupstack: popanddestroy (chocolatetpoint );
Cleanupstack: popanddestroy (sealpoint );
}
We can see that the order of the stack is the opposite of the Order of the stack, but here it is a bit more complicated. The preceding four statements can use cleanupstack :: popanddestroy (4); or cleanupstack: popanddestroy (4, sealpoint); the replacement effect is basically the same.
Comments, easy!
Who will destroy the objects created on the stack?
An object cannot be purged more than once. If there is a pointer to the object on the stack to be cleared and then saved to another place, such as a member variable of another object, this object can still be accessed after it exits unexpectedly, the pointer should pop up from the clear stack. If this pointer is retained on the clear stack, the clear stack will destroy the objects it refers, however, the object that saves the pointer will also try to destroy the object referred to by the pointer through the destructor.
Therefore, the object should only be referenced by the clear stack or another object, but not both. Similarly, never push the class member variables to the clear stack.
Example:
Void transferownershipexamplel ()
{
Cclanger * clanger = new (eleave) cclanger ();
Cleanupstack: pushl (clanger); // press to clear the stack
Imemberobject-> takeownershipl (clanger); // The class member imemberobject obtains the object ownership.
Cleanupstack: Pop (clanger); // The Object Pointer must be popped up from the clear stack.
// After the Exception Code is called, the object pointer will pop up from the clear stack.
}
Comments:Never push the class member variables into the clear stack, which will lead to double-Deletion
Naming questions:
If an object is pushed to the clear stack and is still on the clear stack until the function returns, the function should be suffixed with "C. This tells the function caller that if the function returns normally, there are still unnecessary objects in the stack.
Csiamese * csiamese: newlc (tpointcolor apointcolor)
{
Csiamese * Me = new (eleave) csiamese (apointcolor );
Cleanupstack: pushl (me );
Me-> constructl ();
Return me;
}
The above functions are actually used in the two-phase structure of the object. After being pushed to the clear stack, it does not pop up. Therefore, the name must end with "C.
3. Use clear stack for non-cbase Derived classes
Let's take a look at cleanupstack: pushl () in three heavy load forms:
(1) cleanupstack: pushl (cbase * aptr)
(2) cleanupstack: pushl (Tany *)
(3) cleanupstack: pushl (tcleanupitem)
The first form is used on the cbase derived class object. When popanddestroy is used, the destructor of the derived class object is called, which is the same as C ++, first, call the destructor of the deepest class of the derived hierarchy, call the Destructor sequentially along the derived hierarchy, and finally call the cbase empty destructor.
Second form: if you forget to derive from cbase when defining a class C, it will be very dangerous (never do this. If you do this, what will happen, you have to take full responsibility), because when calling pushl, you actually call the cleanupstack: pushl (Tany *) method, in this way, the destructor of this class will not be called in Pop, just to clear the heap memory that it points. In fact, this method is used to push heap-based object pointers without destructor into the clearing stack (such as t-class objects or struct ).
Form 3: receive a tcleanupitem object as a parameter. This function enables us to push other types of objects or objects with custom cleanup routines to the cleanup stack. The tcleanupitem object encapsulates a pointer to the object to be saved on the clear stack and a function pointer to provide the corresponding object clearing operation.
In addition, Symbian OS provides three utility functions for cleanup: cleanupreleasepushl (), cleanupdeletepushl (), and cleanupclosepushl (), which correspond to release () and delete (), close (), will generate a tcleanupitem object so that we can customize the clearance process. And combine the following three methods into the stack. Of course, the object reference on the stack can be any object created on the stack, such as class C, class R, class T, and class M.
(1) cleanupreleasepushl (T & Aref note that the parameter is not T *)//
The exception exit processing or popanddestroy () call will call release () on T-class objects ().
Example:
Class mextraterrestrial
{
Public:
Virtual void communicatel () = 0;
..... // For cleanliness, skip interface other code
Virtual void release () = 0;
}
Class cclanger: Public cbase, mextraterrestrial
{
Public:
Static mextraterrestrial * newl ();
Virtual void communicatel ();
Virtual void release ();
PRIVATE:
Cclanger ();
~ Cclanger ();
PRIVATE:
........
}
Void testmixinl ()
{
Mexico traterrestrial * clanger = clanger: newl ();
Cleanupreleasepushl (* clanger); // The parameter is not a pointer, which is different from normal pushl.
........ // Execute code that may exit unexpectedly
Cleanupstack: popanddestroy (clanger); // the pointer to the object.
}
Note: The parameters for inbound and outbound stack clearance are different.
(2) cleanupdeletepushl (T & Aref)
By using cleanupdeletepushl (), you can exit the exception processing or use popanddestroy () to call the delete operation on the object, then call the object's destructor, and the corresponding heap memory will be released. This is similar to using the cleanupstack: pushl () overload function that accepts the cbase pointer.This function is particularly useful when the pointer of the M derived class must be placed on the clear stack.
(3) cleanupclosepushl (the close () method is built in the T & Aref class object, so you do not need to add it .) // R
If the object is pushed through cleanupclosepushl () to clear the stack, the exception exit processing or popanddestroy () call will apply the close () operation to the object.
Void usefilesysteml ()
{
RFS thefs;
User: leaveiferror (thefs. Connect ());
Cleanupclosepushl (thefs );
........ // Execute code that may exit unexpectedly
Cleanupstack: popanddestroy (& thefs );
}
The three tool template functions provided by Symbian OS correspond to the three clearing methods: release (), delete (), and close, all three tool functions generate an object of the tcleanupitem type and push it into the clear stack.
Comments: in fact, the above three functions will generate a tcleanupitem object, and then this object will automatically call the corresponding close () or other functions.
Summary:
In the system, each executable unit (or thread) allocated with resources has its own stack clearance and the highest level trap/trapd macro for exception handling and some post-exit operations. Stack cleanup is introduced to solve the heap memory leakage problem. Note that the heap memory is used. If the object is created on the stack, it is not necessary to clear the stack, because the space occupied by objects on the stack is automatically managed by the stack.
Note: For Class C objects, the cleanupstack: Pop () method only pops up class C object pointers from the clear stack, the destructor of this class C object is not called. To perform the destructor, you need to add the delete C statement, or you can directly use cleanupstack: popanddestroy () to complete the preceding two actions at the same time.