• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: test if a pointer is pointing to a valid object or not?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

References: 
 >Re: test if a pointer is pointing to a valid object or not? (From: Ambroise Confetti <email@hidden>)

  • Prev by Date: Blocks in Obj-C (was: Passing Obj-C method as callback to C function)
  • Next by Date: Re: Importing into Xcode
  • Previous by thread: Re: test if a pointer is pointing to a valid object or not?
  • Next by thread: Cross Compiling 10.2 from 10.3 using a Makefile ???
  • Index(es):
    • Date
    • Thread