Академический Документы
Профессиональный Документы
Культура Документы
by Jo Stichbury
When a panic occurs in a secondary thread, it is only The errrd file is usually present by default for the
that thread that closes, unless the thread has been set Windows emulator, but isn’t usually present on phone
as process critical or process permanent, in which case hardware, so if you want to see more information when a
the process also panics (for more information about panic occurs in a thread running on the phone, you need
critical threads, see the API reference information for the to create the file in the c:\resource directory. To do this,
User::SetCritical() method). either use a filesystem browser that is capable of access-
ing that directory and allowing you to create files in it, or
create your own SIS file to install the file to that location.
Comparing Leaves and Panics
As I’ve already mentioned, a panic can’t be trapped There is an exception to the rule that prevents a
and terminates at least the thread in which it occurs (and thread in one process from panicking a thread in a
often the process too). different process. A server thread may use the RMes-
sagePtr2 API to panic a misbehaving client thread,
A leave should always be caught (‘trapped’) by code for example, when a client passes a badly-formed
somewhere in the call stack, because there should request. Rather than go ahead and attempt to read
always be a top-level TRAP. However, if a leave is not or write to a bad descriptor, the server protects itself
caught, this implies that the top-level TRAP is absent (a and flags the problem by causing a panic in the client
programming error) and the thread will be panicked and thread.
terminate. You can find out more about leaves from an
article on the Symbian developer wiki entitled “A Com- Generally, a malformed client request occurs because
parison of Leaves and Exceptions”. of a programming error in client code, but this strategy
also protects a server against more malicious “denial
When to leave? When to panic? of service” attacks in which a client may deliberately
Let’s first make a clear distinction between program- pass a badly-formed or unrecognized request to a
ming errors (“bugs”) and exceptional conditions: server to try to crash it.
Bugs may be contradictory assumptions, unexpected When using panics, it’s good style to make your panic
design errors or genuine implementation errors, such as category string descriptive and unique, so other develop-
writing off the end of an array or trying to write to a file ers using your APIs can locate the string in your header
before opening it. These are persistent, unrecoverable er- files, and with it, any associated panic error codes (which
rors which should be detected and corrected in your code should also have suitably descriptive names).
at the earliest opportunity.
Thus, you might have a general panic header file for
Exceptional conditions are different. They may legiti- your library which includes the following:
mately arise, although it is rare (hence the term ‘excep-
tional’) and they are not consistent with typical or expect-
ed execution. A good example of an exceptional condition // ExamplePanic.h
#ifndef __EXAMPLEPANIC_H_
that may occur on Symbian platform is an out-of-memory
#define __EXAMPLEPANIC_H__
failure. It is not possible to stop exceptions occurring, so #include <e32base.h>
your code should implement a graceful recovery strategy
rather than simply crash and burn. _LIT(KExamplePanic, “EXAMPLE-ENGINE”);
enum TExampleEnginePanic
Consider the following questions. Can a scenario arise {
legitimately? If it can - is there anything you should or ECorrupt, // =0,
could do to handle it? ENotInitialized, // =1,
EInvalidSetting // =2
};
If your answer to both is ‘yes’, you’re looking at an
exceptional condition and should flag it using a leave. static void ExamplePanic(TExampleEnginePanic
aCategory);
If the answer to either (or both) is ‘no’, you should con- #endif // __EXAMPLEPANIC_H__
sider the situation to be caused by a bug which must be
tracked down and fixed. Use a panic (within an assertion) The ExamplePanic() function is defined separately as
to flag it up. follows:
Other reasons for this panic to occur include memory You can find a portfolio of Jo’s work on jostichbury.com
misalignment or execution of an invalid instruction inside and find out more about her services at uk.linkedin.
a system call to the kernel executive. com/in/jostichbury.
E32USER-CBASE 46
Raised by the active scheduler as a result of a stray
signal
E32USER-CBASE 90
Raised by the cleanup stack when the object specified to
be popped off is not the next object on the stack.
USER 11
Raised when an operation to modify a 16-bit descriptor
fails because the operation would cause the descriptor’s
data area to exceed its maximum allocated length.
ALLOC xxxxxxxx
Raised in debug builds when the heap checking macros
(e.g. __UHEAP_MARK/__UHEAP_MARKEND) detect that
more heap cells have been allocated than freed. See
this Eliminating Memory Leaks in Symbian C++ Code for
further information about the macros.
Further Information
• Using assertions to detect bugs in C++
• Symbian C++ miscellany (panics and assertions)