Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Symbian OS Explained - Effective C++ Programming For Smartphones (2005) [eng].pdf
Скачиваний:
60
Добавлен:
16.08.2013
Размер:
2.62 Mб
Скачать

26

LEAVES: SYMBIAN OS EXCEPTIONS

TRAP, even if none of the callers can handle errors and they all call User::LeaveIfError() as shown in the previous code sample.

However, if you know who all your callers are (for example, if the function is internal to your component) and you know that the callers will all TRAP the call, it may be worthwhile implementing the version that returns an error. This limits the use of a TRAP to one place in the code.

Use of TRAP is an expensive way of managing a leave in terms of executable size and execution speed. You should attempt to minimize the number of TRAPs you use in your code where possible. However, every program must have at least one TRAP to catch any leaves that occur.

2.6 LeaveScan

LeaveScan is a useful tool which you should run regularly against your source code. It checks that all functions which have the potential to leave are named according to the Symbian OS convention, with a trailing L. LeaveScan can be used on your source to indicate areas of code where you may have forgotten to use the convention. By revealing where leaves may occur but are not acknowledged by the function name, it highlights potential bugs and gives you an opportunity to fix the problem and ensure that your code handles any leaves correctly.

LeaveScan works by examining each line of source code and checking that functions which do not terminate in L cannot leave. However, there are a few functions (more accurately, operators) in Symbian OS that may leave but cannot have an L suffix (such as operator<< and operator>> for RWriteStream and RReadStream respectively). The naming convention cannot be applied appropriately to operators and, unfortunately, LeaveScan does not have the sophisticated logic needed to recognize operators that may leave. When you use operators that you know have the potential to leave, you’ll have to remember to check this code by sight yourself.

LeaveScan also checks functions which do have a trailing L to see if they really can leave. If functions are encountered which do not leave, LeaveScan raises a warning. However, this scenario can be perfectly valid, for example, when implementing an abstract function such as CActive::RunL(), some implementations may leave but others may not.

SUMMARY

27

LeaveScan will highlight functions which may leave but are incorrectly named without a suffixed ”L”. The potential to leave occurs when a function:

calls other functions which themselves leave (and thus have function names ending in L) but does not surround the function call with a TRAP harness

calls a system function which initiates a leave, such as

User::LeaveIfError() or User::Leave()

allocates an object on the heap using the Symbian OS overload of operator new.

2.7Summary

This chapter discussed leaves, which are the lightweight equivalent of C++ exceptions on Symbian OS. A leave is used to propagate an error which occurs because of exceptional conditions (such as being out of memory or disk space) to higher-level code which can handle it. A Symbian OS leave is equivalent to a C++ throw and a TRAP harness is used to catch the leave. In fact, a TRAP harness is effectively a combination of try and catch.

Having compared leaves and TRAPs with standard C++, it’s worth making a comparison with the standard library too. TRAP and leave are analogous to the setjmp() and longjmp() methods, respectively – setjmp() stores information about the location to be ”jumped to” in a jump buffer, which is used by longjmp() to direct the code to jump to that point.

On Symbian OS, if a function can leave, that is, fail under exceptional conditions, it indicates this by suffixing its function name with L. Of all the Symbian OS naming conventions, this is one you should comply with because, if you don’t, it’s hard to know whether you can call a function without potentially orphaning any local heap-based variables. You can check that you have adhered to the naming convention by running the LeaveScan tool over your source code.

A function can leave if it calls one of the system functions which cause a leave (such as User::Leave()), calls another leaving function (such as NewL()) or allocates memory using the Symbian OS leaving overload of operator new. Some functions should not leave, namely constructors (I’ll discuss the reasons behind this in detail in Chapter 4,

28

LEAVES: SYMBIAN OS EXCEPTIONS

and explain how two-phase construction can be used to prevent it) and destructors, which could potentially leak memory by leaving before completing object cleanup.

This chapter described best practice for writing and calling leaving code, particularly when a function may have a return value for initialized pointer data. It also discussed the best use of the TRAP and TRAPD macros, which can have a significant overhead in terms of code size and runtime speed.

The next chapter discusses the use of the cleanup stack to protect heap-based local variables against orphaning in the event of a leave.