03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

successfully). This means you have to check errors explicitly using<br />

User::LeaveIfError(), which does nothing if its argument is KErrNone or a positive<br />

number, but leaves with the value of its argument if the argument is negative.<br />

A few low-level Symbian OS APIs operate this way; their areas of application are a few<br />

important circumstances in which it would be inappropriate to leave.<br />

� Many file or communications functions are called speculatively to test whether a file is<br />

there or a link is up. It is information, not an error, if these functions return with 'not<br />

found' or a similar result.<br />

� Symbian's implementation of the C Standard Library provides a thin layer over the<br />

native RFile and communications APIs that returns standard C-type error codes. It's<br />

much easier to handle errors directly than by trapping them. In any case, standard<br />

library programs don't have a cleanup stack.<br />

Granted, leaves could have been trapped. But that was judged undesirably expensive.<br />

Instead, when you want a leave, you have to call User::LeaveIfError(). That's a little<br />

bit costly too, but not as expensive as trapping leaves.<br />

Note<br />

Some truly ancient code in Symbian OS was written assuming that there<br />

might not be a cleanup stack at all. But this isn't a sensible assumption these<br />

days, and is certainly no justification for designing APIs that don't use L<br />

functions. All native Symbian OS code should ensure it has a cleanup stack,<br />

and design its APIs accordingly. The GUI application framework, and the<br />

server framework, provide you with a cleanup stack, so you only have to<br />

construct your own when you're writing a text console program.<br />

Important<br />

If you are using components with TInt error codes, don't forget to use<br />

User::LeaveIfError(), where you need it.<br />

6.5.3 R Classes on the Cleanup Stack<br />

Sometimes, you need to create and use an R class object as an automatic variable rather<br />

than as a member of a C class. In this case, you need to be able to push to the cleanup<br />

stack. There are two options available to you:<br />

� Use CleanupClosePushL()<br />

� Do it directly : make a TCleanupItem consisting of a pointer to the R object, and a<br />

static function that will close it.<br />

If you have an item with a Close() function, then CleanupClose-PushL() (a global<br />

nonmember function) will ensure that Close() is called when the item is popped and<br />

destroyed by the cleanup stack. C++ templates are used for this, so Close() does not have<br />

to be virtual.<br />

The code below, taken from memorymagic, demonstrates how to use<br />

CleanupClosePushL():<br />

case EMagicCmdDeleteFile:<br />

{<br />

RFs fs;<br />

CleanupClosePushL(fs);<br />

User::LeaveIfError(fs.Connect());<br />

User::LeaveIfError(fs.Delete(KTextFileName));

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

Saved successfully!

Ooh no, something went wrong!