Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Catching signal errors
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Catching signal errors

Rick Steele wrote:

Forgive the ignorance but,

if one has a function like this within an application:

void SumFunction(){

Ptr pPtr = -1L; //create something that will obviously crash if deferenced
char pSomethingElse;
pSomethingElse = (*pPtr); // Causing the error

how would some one catch the signal that would inevitable occur so the app might be able to close down gracefully?

Try 0 (or NULL) -- it should cause a SIGSEGV or SIGBUS (depending on the platform), whereas -1L could (potentially) be a valid chunk of memory.

How about this snippet:
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>

typedef void (*full_sighandler_t)(int,siginfo_t*,void*);

void installSignalHandler(int sig, full_sighandler_t handler)
   struct sigaction temp;
   sigaction(sig, 0, &temp);
   // Set the SA_SIGINFO flag so that sa_sigaction may be safely used.
   temp.sa_flags = SA_SIGINFO;
   temp.sa_sigaction = handler;
   sigaction(sig, &temp, NULL);

void fatalSigHandler(int sig, siginfo_t* info, void* context)
// We are very limited in what we are allowed to do in a signal handler. // Memory should not be allocated, and most libc functions should not be used.
static char sig_message[] = "Aborting: Fatal signal occurred!\n";

   write(1, sig_message, sizeof(sig_message));

int main(int argc, char** argv)
   installSignalHandler(SIGSEGV, &fatalSigHandler);
   installSignalHandler(SIGBUS, &fatalSigHandler);

   int* foo = NULL;

   *foo = 0;

You should read the man pages for signal and sigaction.


Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

 >Catching signal errors (From: Rick Steele <email@hidden>)

Visit the Apple Store online or at retail locations.

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.