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.

{<br />

__UHEAP_MARK;<br />

__UHEAP_SETFAIL(RHeap::EFailNext, i);<br />

TRAPD(error, TestFunctionL());<br />

__UHEAP_MARKEND;<br />

TAny* testAlloc = User::Alloc(1);<br />

TBool heapTestingComplete = (testAlloc == NULL);<br />

User::Free(testAlloc);<br />

if ((error != KErrNoMemory) && heapTestingComplete)<br />

break;<br />

}<br />

This loop runs the test function in such a way that each time through, one more successful<br />

heap allocation is allowed. It is guaranteed that there will be a KErrNoMemory error (if<br />

nothing else) that will cause a leave and cleanup. Any failure to clean up properly will be<br />

caught by the heap-balance checks. The loop terminates when the test function has<br />

completed without generating a memory-allocation failure.<br />

6.9 Summary<br />

Memory and other resources are scarce in typical Symbian OS environments. Your<br />

programs will encounter resource shortages, and must be able to deal with them. You must<br />

avoid memory leaks, both under normal circumstances and when dealing with errors.<br />

Symbian OS provides an industrial-strength framework to support you, with very low<br />

programmer overhead, and very compact code.<br />

In this chapter, we've seen the following:<br />

� How the debug keys, and their simulated failures, and the heap- checking tools built<br />

into CONE, mean that testing your code is easy.<br />

� How naming conventions help with error handling and cleanup.<br />

� Allocating and destroying objects on the heap – how to preserve heap balance, what to<br />

do if allocation fails, how to reallocate safely.<br />

� How leaves work, the TRAP() and TRAPD() macros, and Symbian OS error codes.<br />

� When to use your own traps.<br />

� When and how to use the cleanup stack.<br />

� Two-phase construction, using ConstructL() and the NewL() and NewLC() factory<br />

functions.<br />

� What CBase provides for any C class to help with cleanup.<br />

� Cleanup for R and T classes.<br />

� How to panic a program, and test engines and libraries.<br />

I've covered a lot of material in this chapter, and you could be forgiven for not taking it all in<br />

at once. Don't worry – if you only remember one thing from this chapter, remember that<br />

cleanup is vital. You'll see cleanup-related disciplines in all the code throughout the rest of<br />

the book, and you'll be able to come back here for reference when you need to.

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

Saved successfully!

Ooh no, something went wrong!