Nokia official training (Symbian 4300) Notes (5)-memory management

Source: Internet
Author: User

Why Memory ManagementSymbian OS is developed for devices with limited memory and resources. When an application is running, memory usage or hardware resource unavailability may occur. Such exceptions cannot be solved by modifying the program. Therefore, follow the following rules:
  • Try not to use unnecessary Ram
  • Release resources as soon as possible, such as file server
  • Each time you apply for memory, you must prepare to handle out-of-memory errors.
  • When an out-of-memory error occurs, the system returns to a stable State and releases all resources applied during the period.
Stack and heapSTACK: the default size is 8 KB, which is automatically deleted, for example, tint I = 0; heap: at least 0.5 Mb, Which is manually deleted by the programmer, for example, cmyobj * OBJ = new (eleave) cmyobj; LeavesFirst, we will introduce conventional C ++ memory management. In Symbian's view, this is very inefficient.
  • Null Pointer checking if (myobj = new cmyobj () = NULL) {// error handling}
  • Ansi c ++ exeption handling try {// throw an exception} catch (int e) {// error handling}
Leave is recommended in Symbian. If the memory or resources cannot be allocated, this code will leave along the call stack until the operating system or handle is dropped in a function. It is best to end all functions that may have leave with L, so that the user of this function knows that this function may have leave. Leave example:
  • Dynamic Memory Allocation: return New (eleave) tuint8 [1000];
  • Generate a leave: User: Leave (kerrnotfound );
  • Leave: User: leavenomemory ();
  • When null, leave: User: leaveifnull (anotify );
  • When an error occurs, leave: rfs fs; tint err = FS. Connect (); User: leaveiferror (ERR );
Handle leave: the operating system has the default leave processing method:
  • When the program starts: directly close the application.
  • After the application is started: an error message is displayed.
Developers can use trap devices to handle leave. Trap (_ r, _ s) and trapd (_ r, _ s), where:
  • _ R: The leave code of the tint type. The default value is terrnone.
  • _ S: A series of C ++ statements that may be leave.
Trapd (ERR, dofunctionl ());
If (Err! = Kerrnone)
{// Error handling}
Else
{// Everything is well} The cleanup StackCleanup stack is used to store local variables (pointers) that require deallocating after leave occurs ). That is, when a function leave, all objects on the cleanup stack will be deleted. Cleanup stack usage: cleanupstack: pushl (PTR): When leave occurs, all memory will be released.
Cleanupclosepushl (handle): When leave occurs, the handler will be disabled. cleanupstack: Pop (pointer): the first element is output from the stack.
Cleanupstack: popanddestroy (pointer): the first element goes out of the stack and releases the memory. If a function may be leave, check the two conditions:
  • If leave exists, whether all the elements on the heap are in the cleanup stack.
  • If you do not have leave, do you want to properly clean it?
Cmyclass * cmyclass: newl (tint abufsize)
{
Cmyclass * Self = new (eleave) cmyclass;
Cleanupstack: pushl (Self );
Self-> constructl (abufsize );
Cleanupstack: Pop (Self );
Return self;
} If a function leaves an object on the cleanup stack, it must end with C. Two Phase ConstructionC ++ constructor must not be leave. All memory and resource allocation should be completed in phase 2 constructl. Coding guide. All user-defined C classes must:
  • The newl and newlc functions are defined as public static.
  • Define constructl and C ++ constructor as private
Best practiseConstruction rules:
  • The default C ++ constructor cannot contain leave code.
  • The leave function may occur and must be called in constructl.
  • If the base class also has constructl, it must be called first. Do not forget to explain it scoping
Destruction rules:
  • Class C must delete its own objects in the destructor.
  • After deleting an object, set its pointer to null.
  • Do not delete objects not owned by this class.
  • Delete the object before reallocation and set its pointer to null.
Further discussion:
  • Preserve stack memory: each process has only 8 KB and parameters are passed in reference mode. large objects are placed on the stack.
  • Preallocation vs last moment allocation: Generally, resources are allocated before use and released immediately after use. However, the advantage of preallocation is that it saves processing time and runs normally without memory (resources have been allocated)
  • Where to put trap harness: the most basic case is to rely on the framework of the GUI application. You can customize the granularity based on different applications.
  • Error Code returns vs. Leaving functions: Check whether a problem occurs before executing a processing operation. The following code:
User: leaveiferror (FS. Connect ()); Memory leaksIf your program has memory leakage, it will crash when it is disabled on the simulator. Discover and solve your memory leakage as early as possible, because you can trace the code changes that may cause memory leakage. If you cannot find it, you can use the following method: heap balance checking:
  • _ Uheap_mark
  • _ Uheap_markend
Use these two macros to place them at the beginning and end of the code you want to check. If Panic occurs, memory leakage occurs in the code. Can be nested. PanicsPanic is an unprocessed exception, which implies an error that cannot be solved. General programs have the following three types of errors:
  • Program error: Reference an element that exceeds the array range
  • Environment error: memory, insufficient disk space, or lack of other resources
  • User error: Enter Error Data
Trap and cleanup stack can be used to solve environment and user errors, but we cannot recover the first type of program errors. It is best to use the user: panic () function, it has two parameters: string and tint.

 

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.