【问题标题】:[UIActivityIndicatorView release]: message sent to deallocated instance in swift-3[UIActivityIndi​​catorView 发布]:消息发送到 swift-3 中已释放的实例
【发布时间】:2017-07-24 07:44:31
【问题描述】:

我在我的 api 调用机制中使用了一个活动指示器,在隐藏 HUD 时代码崩溃并出现“[UIActivityIndi​​catorView release]: message sent to deallocated instance”的错误

这里是堆栈:

 * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x18a5e6ea8)
  * frame #0: 0x000000018a5e6ea8 CoreFoundation`___forwarding___ + 744
    frame #1: 0x000000018a4e2d4c CoreFoundation`_CF_forwarding_prep_0 + 92
    frame #2: 0x000000018a4c2a80 CoreFoundation`-[__NSArrayI dealloc] + 84
    frame #3: 0x0000000189062134 libobjc.A.dylib`(anonymous namespace)::AutoreleasePoolPage::pop(void*) + 836
    frame #4: 0x0000000190711c3c UIKit`-[UIView dealloc] + 1604
    frame #5: 0x0000000189062134 libobjc.A.dylib`(anonymous namespace)::AutoreleasePoolPage::pop(void*) + 836
    frame #6: 0x000000018a4beb28 CoreFoundation`_CFAutoreleasePoolPop + 28
    frame #7: 0x000000018a58ecec CoreFoundation`__CFRunLoopRun + 1580
    frame #8: 0x000000018a4beda4 CoreFoundation`CFRunLoopRunSpecific + 424
    frame #9: 0x000000018bf28074 GraphicsServices`GSEventRunModal + 100
    frame #10: 0x0000000190779058 UIKit`UIApplicationMain + 208
    frame #11: 0x0000000100147448 GraspIO-Dev`main at AppDelegate.swift:14
    frame #12: 0x00000001894cd59c libdyld.dylib`start + 4

我不明白我在犯什么错误,这是我的显示和隐藏代码。

func execute( _ params : String..., serverResponse: @escaping (ServerResponseModel) -> Void) {
        //print("params:\(params)");

        if self.mCallingView != nil {
            DispatchQueue.main.async{
                self.showLoadingHUD()
            }
          }
        }

        makeWebServiceCall(params){
            response in
            //print("\n\n response: \(response)")
            DispatchQueue.main.async { 
                 serverResponse(response!)
                if self.mCallingView != nil {
                  self.hideLoadingHUD()   
                }
            }
        }
    }

    fileprivate func showLoadingHUD() {
        let hud = MBProgressHUD.showAdded(to: mCallingView!, animated: true)
        hud.label.text = "Please wait..."
    }

    fileprivate func hideLoadingHUD() {
        let _ = MBProgressHUD.hide(for: mCallingView!, animated: true)
        mCallingView!.isUserInteractionEnabled = true
        mCallingView = nil
    }

隐藏HUD时应用会崩溃,研究了堆栈溢出,但找不到解决方案。

【问题讨论】:

  • 是“makeWebServiceCall”这个类的属性作为强参考吗?如果是这样 - 你需要添加 [weak self] 到它
  • 弱引用@GrzegorzKrukowski
  • 你能把你所有的属性贴出来吗?标签是什么样子的,进度条是什么样子的?
  • 在 swift 中试用这个适用于 iOS 的 HUD 库 github.com/shubh10/JustHUD

标签: swift memory-management uiactivityindicatorview dealloc mbprogresshud


【解决方案1】:

该错误非常具有描述性。 您正在尝试设置已经发布的 nil 。 现在你需要找到你正在做的地方以及避免这种情况的方法。正如你所提到的,你在隐藏它的同时得到了这个。所以你已经在这个问题上归零了。

fileprivate func hideLoadingHUD() {
    let _ = MBProgressHUD.hide(for: mCallingView!, animated: true)
    mCallingView!.isUserInteractionEnabled = true
    mCallingView = nil
}

这里。您正在尝试制作mCallingView = nil。删除此行并检查是否收到错误。您真的需要在隐藏活动指示器的同时将源视图设为 nil 吗?

【讨论】:

    猜你喜欢
    • 2011-06-16
    • 2011-08-16
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多