Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling DDLogError on background thread causes EXC_BAD_INSTRUCTION #108

Closed
brewpoo opened this issue Apr 12, 2013 · 6 comments
Closed

Calling DDLogError on background thread causes EXC_BAD_INSTRUCTION #108

brewpoo opened this issue Apr 12, 2013 · 6 comments

Comments

@brewpoo
Copy link

brewpoo commented Apr 12, 2013

I know CocoaLumberjack is thread safe but I have been having this issue. Basically, I spawn off a thread using GCD

    dispatch_queue_t queue = dispatch_queue_create("somethread-name", NULL);
    dispatch_queue_t main = dispatch_get_main_queue();
    dispatch_async(queue, ^{
        NSDictionary *response = [WebServiceController getData];
        dispatch_async(main, ^{
            [self processResponse:response];
        });
    });

Within [WebServiceController getData] I call DDLogError when there is an error and this throws EXC_BAD_INSTRUCTION when running in the Simulator. On a device it throws EXC_BREAKPOINT(code=EXC_ARM_BREAKPOINT, subcode=0xdefe). Any suggestions for troubleshooting the issue?

@brewpoo
Copy link
Author

brewpoo commented Apr 15, 2013

Found a workaround by turning on exception break point. Was throwing error in DDLog.m:

#pragma clang diagnostic push
        #pragma clang diagnostic ignored "-Wdeprecated-declarations"
        // The documentation for dispatch_get_current_queue() states:
        //
        // > [This method is] "recommended for debugging and logging purposes only"...
        //
        // Well that's exactly how we're using it here. Literally for logging purposes only.
        // However, Apple has decided to deprecate this method anyway.
        // However they have not given us an alternate version of dispatch_queue_get_label() that
        // automatically uses the current queue, thus dispatch_get_current_queue() is still required.
        // 
        // If dispatch_get_current_queue() disappears, without a dispatch_queue_get_label() alternative,
        // Apple will have effectively taken away our ability to properly log the name of executing dispatch queue.

        dispatch_queue_t currentQueue = dispatch_get_current_queue();  // <- CRASHES HERE
        #pragma clang diagnostic pop

        queueLabel = dd_str_copy(dispatch_queue_get_label(currentQueue));

        threadName = [[NSThread currentThread] name];

Changed to:

#pragma clang diagnostic push
        #pragma clang diagnostic ignored "-Wdeprecated-declarations"
        // The documentation for dispatch_get_current_queue() states:
        //
        // > [This method is] "recommended for debugging and logging purposes only"...
        //
        // Well that's exactly how we're using it here. Literally for logging purposes only.
        // However, Apple has decided to deprecate this method anyway.
        // However they have not given us an alternate version of dispatch_queue_get_label() that
        // automatically uses the current queue, thus dispatch_get_current_queue() is still required.
        // 
        // If dispatch_get_current_queue() disappears, without a dispatch_queue_get_label() alternative,
        // Apple will have effectively taken away our ability to properly log the name of executing dispatch queue.

        //dispatch_queue_t currentQueue = dispatch_get_current_queue();
        #pragma clang diagnostic pop

        //queueLabel = dd_str_copy(dispatch_queue_get_label(currentQueue));

        threadName = [[NSThread currentThread] name];

And no longer crashes...

@desplesda
Copy link

I'm getting this error as well, and my workaround was the same.

I just tested this:

dispatch_queue_t backgroundQueue = dispatch_queue_create("Background Queue", DISPATCH_QUEUE_SERIAL);
dispatch_async(backgroundQueue, ^{
    dispatch_get_current_queue();
});

It crashes on the call to dispatch_get_current_queue(). So I don't think that the error is in CocoaLumberjack, but rather in GCD itself.

@desplesda
Copy link

A followup: if I get a queue using dispatch_get_global_queue, rather than creating a new one, the crash doesn't occur.

kolyuchiy pushed a commit to denivip/CocoaLumberjack that referenced this issue Jun 12, 2013
@sourabhletsgomo
Copy link

const double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);

__block GSCall *call_ = _call;
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

    [call_ begin];
});

after this it gives error
screen shot 2013-07-01 at 1 21 28 pm

@dan55304
Copy link

I too have this problem. Is commenting out the queueLabel line the preferred solution? What's the effect of doing this?
** Update: I still get the crash even if commenting out the queueLabel line

bpoplauschi added a commit that referenced this issue Nov 1, 2013
Fix dispatch_get_current_queue crash (Issue #108)
@bpoplauschi
Copy link
Member

#121 should fix this

ewanmellor pushed a commit to tipbit/CocoaLumberjack that referenced this issue Nov 4, 2013
ide added a commit to ide/react-native that referenced this issue Jul 4, 2015
…UEUE_LABEL

I encountered a crash when `RCTCurrentThreadName` called `dispatch_get_current_queue`. There are reports of it crashing e.g. CocoaLumberjack/CocoaLumberjack#108 so better not to call it at all, plus it is deprecated.

Since we still want helpful debugging information, use `DISPATCH_CURRENT_QUEUE_LABEL` instead. It's kind of strange that this constant is defined to be NULL and the docs for `dispatch_get_queue_label` say not to pass in NULL, but in practice `DISPATCH_CURRENT_QUEUE_LABEL` is provided by the iOS SDK and works correctly.

Test Plan: Set a breakpoint inside of `RCTCurrentThreadName` and inspect the name of the thread returned from `dispatch_get_queue_label`. Get valid-looking values like "com.facebook.React.RCTSettingsManagerQueue".

Tested Cmd-R for good measure. The app refreshed fine without any crashes.
a2 pushed a commit to a2/react-native that referenced this issue Jul 7, 2015
…UEUE_LABEL

Summary:
I encountered a crash when `RCTCurrentThreadName` called `dispatch_get_current_queue`. There are reports of it crashing e.g. CocoaLumberjack/CocoaLumberjack#108 so better not to call it at all, plus it is deprecated.

Since we still want helpful debugging information, use `DISPATCH_CURRENT_QUEUE_LABEL` instead. It's kind of strange that this constant is defined to be NULL and the docs for `dispatch_get_queue_label` say not to pass in NULL, but in practice `DISPATCH_CURRENT_QUEUE_LABEL` is provided by the iOS SDK and works correctly.

Closes facebook#1868
Github Author: James Ide <ide@jameside.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants