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

  • SEARCH
  • Home
  • 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 6801235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:04:31+00:00 2026-05-26T19:04:31+00:00

I know how to resolve EXC_BAD_ACCESS issues, but I’m not sure how to unit

  • 0

I know how to resolve EXC_BAD_ACCESS issues, but I’m not sure how to unit test for it. Is there a way to capture EXC_BAD_ACCESS in code instead of simply crashing?

Here’s why I ask: I have written a library that heavily uses blocks, like this:

- (void)doSomething:(void (^)())myBlock;

In my implementation of doSomething: I’m going to eventually run the block, like this:

myBlock();

If a caller passes nil for the block, then it will crash with EXC_BAD_ACCESS, so the solution is to check that the block exists, like this:

if (myBlock) {
    myBlock();
}

This nil check is pretty easy to forget, so I’d like a way to write a unit test that fails when the crash occurs. I suppose a crash could be considered a test failure, but I think it would be nicer for others trying to run the tests to see a nice failure message rather than a crash. Any ideas?

  • 1 1 Answer
  • 0 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. Editorial Team
    Editorial Team
    2026-05-26T19:04:31+00:00Added an answer on May 26, 2026 at 7:04 pm

    I think you’ll need to run the test in a subprocess; then you can let the subprocess crash, check for that crash, and fail the test neatly if it occurs.

    Working from Peter Hosey’s singleton test code.

    - (void) runTestInSubprocess:(SEL)testCmd {
            pid_t pid = fork();
            // The return value of fork is 0 in the child process, and it is
            // the id of the child process in the parent process.
            if (pid == 0) {
                // Child process: run test
                // isInSubprocess is an ivar of your test case class
                isInSubprocess = YES;
                [self performSelector:testCmd];
                exit(0);
            } else {
                // Parent process: wait for child process to end, check 
                // its status
                int status;
                waitpid(pid, &status, /*options*/ 0);
                // This was a crash; fail the test
                STAssertFalse(WIFSIGNALED(status), @"Test %@ crashed due to signal %d", NSStringFromSelector(testCmd), WTERMSIG(status));
            }
    }
    

    Each test will then run itself in a subprocess like so:

    - (void) testSomething {
        if (!isInSubprocess) {
                // Hand off this test's selector to be run in a subprocess
                [self runTestInSubprocess:_cmd];
                return;
        }
    
        // Put actual test code here
        STAssertEquals(1, 1, @"Something wrong with the universe.");
    
    }
    

    You may need to tweak this; I haven’t tested it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a problem that don't know how to resolve only with SQL (I
I want to make my architecture better but i don't know how to resolve
Know this might be rather basic, but I been trying to figure out how
I know what I asking might not make a lot of sense for C#
I know that it's a subject that can raise a lot of debate, but
I know this is fairly subjective, but I'm diving into testing and learning about
I know this question has kind-a started religious wars in past and there might
I know this is largely an opinion, but I'm interested if you have one
There is a simple way with php , a simple script or URL manipulation
I have a big problem to resolve and I don't know how to resolve

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.