Re: test if a pointer is pointing to a valid object or not?
Re: test if a pointer is pointing to a valid object or not?
- Subject: Re: test if a pointer is pointing to a valid object or not?
- From: "Alastair J.Houghton" <email@hidden>
- Date: Wed, 29 Oct 2003 15:54:22 +0000
On Wednesday, October 29, 2003, at 03:22 pm, Ambroise Confetti wrote:
Le 29 oct. 03, ` 12:52, Alastair J.Houghton a icrit :
You still need to do OS calls, or attempt to access it and catch
SIGSEGV and SIGBUS.
How would you do that?
Any good book on UN*X or POSIX programming will tell you. Basically,
you can do something like this:
#include <setjmp.h>
#include <signal.h>
jmp_buf return_location;
void handle_crash(int sig_num)
{
longjmp(return_location, 1);
}
then, around the code you expect to crash, you write something like the
following:
void (*old_segv)(int);
void (*old_bus)(int);
old_segv = signal(SIGSEGV, handle_crash);
old_bus = signal(SIGBUS, handle_crash);
if (!setjmp (return_location)) {
/* Write the code that might crash in here */
} else {
/* We only get here if there was a crash */
NSLog (@"We crashed!\n");
}
signal(SIGSEGV, old_segv);
signal(SIGBUS, old_bus);
The only requirement on where you can return to is that the function
that called setjmp() must not have returned when the longjmp() is
called (otherwise the stack would be trashed, and you'd crash).
See "man setjmp" and "man signal" for more information.
It isn't really a very good idea to employ this sort of code unless
you're certain you know what you're doing. Any library called during
the crash could easily be left in an invalid state, so your application
may very well crash immediately anyway.
Kind regards,
Alastair.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.