<tfoot id='TP6KL'></tfoot>

    <small id='TP6KL'></small><noframes id='TP6KL'>

    <legend id='TP6KL'><style id='TP6KL'><dir id='TP6KL'><q id='TP6KL'></q></dir></style></legend>
    • <bdo id='TP6KL'></bdo><ul id='TP6KL'></ul>

    1. <i id='TP6KL'><tr id='TP6KL'><dt id='TP6KL'><q id='TP6KL'><span id='TP6KL'><b id='TP6KL'><form id='TP6KL'><ins id='TP6KL'></ins><ul id='TP6KL'></ul><sub id='TP6KL'></sub></form><legend id='TP6KL'></legend><bdo id='TP6KL'><pre id='TP6KL'><center id='TP6KL'></center></pre></bdo></b><th id='TP6KL'></th></span></q></dt></tr></i><div id='TP6KL'><tfoot id='TP6KL'></tfoot><dl id='TP6KL'><fieldset id='TP6KL'></fieldset></dl></div>
    2. 暂停计时器上的 dispatch_source_cancel 导致 EXC_BAD_I

      时间:2023-05-31

        <small id='UACsu'></small><noframes id='UACsu'>

          <tfoot id='UACsu'></tfoot>
          <legend id='UACsu'><style id='UACsu'><dir id='UACsu'><q id='UACsu'></q></dir></style></legend>
          • <bdo id='UACsu'></bdo><ul id='UACsu'></ul>
                <tbody id='UACsu'></tbody>
              • <i id='UACsu'><tr id='UACsu'><dt id='UACsu'><q id='UACsu'><span id='UACsu'><b id='UACsu'><form id='UACsu'><ins id='UACsu'></ins><ul id='UACsu'></ul><sub id='UACsu'></sub></form><legend id='UACsu'></legend><bdo id='UACsu'><pre id='UACsu'><center id='UACsu'></center></pre></bdo></b><th id='UACsu'></th></span></q></dt></tr></i><div id='UACsu'><tfoot id='UACsu'></tfoot><dl id='UACsu'><fieldset id='UACsu'></fieldset></dl></div>
                本文介绍了暂停计时器上的 dispatch_source_cancel 导致 EXC_BAD_INSTRUCTION的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                I'm trying to cancel and then release a suspended timer but when I invoke 'dispatch_release' on it, I immediately get EXC_BAD_INSTRUCTION.

                Is this not a valid set of actions to take on a timer?

                Timer creation & suspension:

                @interface SomeClass: NSObject { }
                @property (nonatomic, assign) dispatch_source_t             timer;
                @end
                
                // Class implementation
                @implementation SomeClass
                
                @synthesize timer = _timer;
                
                - (void)startTimer 
                {
                    dispatch_queue_t globalQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
                    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 
                                                    0, 0, globalQ); 
                
                    dispatch_time_t startWhen = dispatch_walltime(DISPATCH_TIME_NOW, NSEC_PER_SEC * 1);
                    dispatch_source_set_timer(_timer, startWhen, 1 * NSEC_PER_SEC, 5000ull);
                
                    dispatch_source_set_event_handler(_timer, ^{
                        // Perform a task 
                
                        // If a particular amount of time has elapsed, kill this timer
                        if (timeConstraintReached)
                        {
                            // Can I suspend this timer within it's own event handler block?
                            dispatch_suspend(_timer);
                        }
                    });
                
                    dispatch_resume(_timer);
                }
                
                - (void)resetTimer
                {
                    dispatch_suspend(_timer);
                
                    dispatch_source_cancel(_timer);
                
                    // dispatch_release causes 
                    // 'EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
                    dispatch_release(_timer);
                
                    self.timer = nil;    
                }
                @end
                

                Additionally, can I invoke dispatch_suspend within a timer source's event_handler block?

                Any help would be appreciated.

                解决方案

                The reason it crashes is because of this code:

                void
                _dispatch_source_xref_release(dispatch_source_t ds)
                {
                    if (slowpath(DISPATCH_OBJECT_SUSPENDED(ds))) {
                        // Arguments for and against this assert are within 6705399
                        DISPATCH_CLIENT_CRASH("Release of a suspended object");
                    }
                    _dispatch_wakeup(ds);
                    _dispatch_release(ds);
                }
                

                So, you can't release a dispatch_source_t that has been suspended. You probably want to just not suspend it in resetTimer I guess.

                Whilst I can't find anything in the docs for why they have written it like this (and the comment alludes to the pros and cons being in a radar we'll never see), all I can do is refer to the docs where it says:

                You can suspend and resume the delivery of dispatch source events temporarily using the dispatch_suspend and dispatch_resume methods. These methods increment and decrement the suspend count for your dispatch object. As a result, you must balance each call to dispatch_suspend with a matching call to dispatch_resume before event delivery resumes.

                Whilst that doesn't say you can't release a dispatch source that's been suspended, it does say you have to balance each call so I'm assuming it's something along the lines of it's using a dispatch semaphore under-the-hood which have to be balanced before they can be released. That's just my guess though :-).

                As for "can I invoke dispatch_suspend within a timer source's event_handler block". I'm pretty sure you can, yes, as per the docs for dispatch_suspend:

                The suspension occurs after completion of any blocks running at the time of the call.

                这篇关于暂停计时器上的 dispatch_source_cancel 导致 EXC_BAD_INSTRUCTION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:最新的 iOS 版本分配百分比? 下一篇:为什么设置断点时我的 xcode 会跳转到汇编视图

                相关文章

                最新文章

                      <bdo id='eHiF1'></bdo><ul id='eHiF1'></ul>

                    <small id='eHiF1'></small><noframes id='eHiF1'>

                  1. <i id='eHiF1'><tr id='eHiF1'><dt id='eHiF1'><q id='eHiF1'><span id='eHiF1'><b id='eHiF1'><form id='eHiF1'><ins id='eHiF1'></ins><ul id='eHiF1'></ul><sub id='eHiF1'></sub></form><legend id='eHiF1'></legend><bdo id='eHiF1'><pre id='eHiF1'><center id='eHiF1'></center></pre></bdo></b><th id='eHiF1'></th></span></q></dt></tr></i><div id='eHiF1'><tfoot id='eHiF1'></tfoot><dl id='eHiF1'><fieldset id='eHiF1'></fieldset></dl></div>
                    <tfoot id='eHiF1'></tfoot>
                    <legend id='eHiF1'><style id='eHiF1'><dir id='eHiF1'><q id='eHiF1'></q></dir></style></legend>