Symbian
Symbian OS Library

FAQ-0929 Why does my code suffer an access violation (Kern-Exec 3) when using setjmp() and longjmp()?

[Index][spacer] [Previous] [Next]



 

Classification: C++ Category: STDLIB
Created: 09/25/2003 Modified: 09/30/2003
Number: FAQ-0929
Platform: Not Applicable

Question:
The standard library functions setjmp() and longjmp() are sometimes used to provide error handling routines in code written to utilize the C standard library, in a similar way to TRAP() and Leave in Symbian C++.

The setjmp() function is called to assign information about the location to be 'jumped to' into to a jump buffer. When longjmp() is called, the point of execution 'jumps' to the location specified in the jump buffer.

Often Kern-Exec 3 errors are seen on hardware just after calling longjmp().

The source of the Kern-Exec 3 error, is an access violation.


Answer:
You need to be careful with register allocated variables that are defined before setjmp() is called, and are subsequently used following the 'second' return from setjmp() (i.e. when longjmp() is invoked).
The problem is that the way the longjmp/leave works is by restoring the register state to be exactly that when setjmp() was called - which means that local variables stored in registers can be 'stomped' by the longjmp process.

There is a documented way to ensure the correct behaviour: make these particular local variables volatile. C++ compilers must handle variables with this qualifier in a way that ensures correct behaviour for setjmp/longjmp.