Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 140067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:32:53+00:00 2026-05-11T07:32:53+00:00

How can I efficiently catch and handle segmentation faults from C in an OSX

  • 0

How can I efficiently catch and handle segmentation faults from C in an OSX Carbon application?

Background: I am making an OSX Carbon application. I must call a library function from a third party. Because of threading issues, the function can occasionally crash, usually because it’s updating itself from one thread, and it’s got some internally stale pointer or handle as I query it from another. The function is a black box to me. I want to be able to call the function but be able to ‘catch’ if it has crashed and supply an alternative return. In Windows, I can use the simple Visual C and Intel C compiler’s __try{} and __except.

/* Working Windows Example */ __try { x=DangerousFunction(y);} __except(EXCEPTION_EXECUTE_HANDLER) {x=0.0;} /* whups, func crashed! */ 

I am trying to make the same kind of crash-catcher for OSX. I am using pure C on a very large application. I call the function millions of times per second, so efficiency is very important too. (Impressively, the Windows __try() overhead is immeasurably small!)

Here’s what I have experimented with:

1) C++ exceptions. I am not sure if C++ exceptions catch the segfault crashes. And my app is currently C. I could try wrappers and #ifdefs to make it C++ but this is a lot of work for the app, and I don’t think C++ exceptions will catch the crash.

2) signal + setjump + longjmp. I thought this would work… it’s what it’s designed for. But I set up my SEGV error handler [in fact I set it up for every signal!] and it’s never called during the crash. I can manually test (and succeed) when calling raise(SEGV). But the crashes don’t seem to actually call it. My thoughts are that CFM applications do NOT have access to the full BSD signals, only a subset, and that Mach applications are necessary for the Real Thing.

3) MPSetExceptionHandler. Not well documented. I attempted to set a handler. It compiled and ran, but did not catch the segfault.

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:32:54+00:00Added an answer on May 11, 2026 at 7:32 am

    Are you sure you’re not getting a SIGBUS rather then a SIGSEGV?

    The below catches SIGBUS as caused by trying to write at memory location 0:

    cristi:tmp diciu$ cat test.c  #include <signal.h>  static void sigac(int sig) {     printf('sig action here, signal is %d\n', sig);     exit(1); }  int main() {     (void)signal(SIGSEGV, sigac);     (void)signal(SIGBUS, sigac);      printf('Raising\n');     strcpy(0, 'aaksdjkajskd|'); }    cristi:tmp diciu$ ./a.out  Raising sig action here, signal is 10 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can mysql handle a dataset of 50gb (only text) efficiently ? If not, what
How can I efficiently and easily sort a list of tuples without being sensitive
I would like some container that I can very efficiently append a variable amount
Is there anything wrong with this code or can this be done more efficiently?
How can you make the efficient many-to-many -relation from fileID to Words and from
I understand that the following code can (perhaps not very efficiently) find out a
I don't really understand how ObjC can efficiently rely on messages. Does this mean
Problem: I would like to catch any exceptions from any method in a class
How do I implement a FIFO buffer to which I can efficiently add arbitrarily
Splitting up functions, into smaller sub function into the code, can effect efficiency of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.