03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Even user input errors can be handled using the cleanup framework: as an example, code<br />

that processes the OK button on a dialog can allocate many resources before finding that an<br />

error has occurred. Dialog code can use the cleanup framework to flag an error and free the<br />

resources with a single function call.<br />

There's just one kind of error that the cleanup framework can't deal with – programming<br />

errors. If you write a program with an error, you have to fix it. The best service Symbian OS<br />

can do you (and your users) is to kill your program as soon as possible when the error is<br />

detected, with enough diagnostics to give you a chance to identify the error and fix it –<br />

hopefully, before you release the program. In Symbian OS, this is a panic. We've heard<br />

about panics already – in this chapter, I'll explain them.<br />

6.2 Handling Out-of-memory Errors<br />

For application programmers, the resources you use mostly consist of the memory in your<br />

own application. With that in mind, we'll introduce the cleanup tools in the context of dealing<br />

with out-of-memory conditions in your own application (or library) code. We'll get confident<br />

with OOM handling, and then we'll look at handling cleanup with other kinds of resources.<br />

The toolkit provided by Symbian OS for handling out of memory and for testing your OOM<br />

handling includes the following:<br />

� The UIQ GUI framework's debug keys.<br />

� Heap-checking tools, which check that all resources that were allocated by a function<br />

are also freed.<br />

� Proper use of the C++ destructor to destroy any owned objects.<br />

� Heap-failure tools, which produce deliberate out-of-memory errors for testing purposes.<br />

� The leave mechanism, which is used to indicate an error. The fundamental function<br />

here is User::Leave(), which is at the heart of any leaving function. It does a job<br />

similar to C++'s and Java's throw.<br />

� The cleanup stack : objects on the cleanup stack are deleted when a leave occurs.<br />

� The trap harness: leave processing is caught by a trap. A leave aborts its function and<br />

any functions that called it, up to and excluding the first function that contains a trap<br />

harness. The trap harness does a similar job to C++'s and Java's try-catch<br />

mechanism.<br />

� The CBase class, the ultimate base class of all C classes, which is recognized by the<br />

cleanup stack, and includes a virtual C++ destructor so that any C class can be cleaned<br />

up using the cleanup stack.<br />

� The two-phase construction pattern necessary to ensure that C++ constructors<br />

never leave. C++ constructors must, therefore, perform only basic initialization. CBasederived<br />

classes should include a ConstructL() function that will complete the<br />

initialization of the object.<br />

� Naming conventions that indicate class resource-allocation patterns, whether a<br />

function has the potential to leave, and other behavior important for cleanup. These<br />

conventions go together with some rules that make it easy to address cleanup<br />

requirements in the vast majority of cases.<br />

The toolkit may look strange at first, even if you have used another error-handling<br />

framework. Actually, it's quite easy to work with. It makes it possible to build very robust<br />

applications with only a few disciplines, and almost no programming overhead.<br />

6.2.1 Uikon Debug Keys<br />

Some of the memory management tools are quite conveniently accessible through debug<br />

builds of the Uikon GUI. Boot up the debug-mode emulator

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!