[UIActivityIndicatorView release]: message sent to deallocated instance in swift-3

442 Views Asked by At

I'm using an activity indicator in mine api calling mechanism, while hiding the HUD the code gets crash and shooting an error of "[UIActivityIndicatorView release]: message sent to deallocated instance".

Here is stack:

 * 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

I couldn't get what's the mistake I'm making and here is mine show and hide code.

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
    }

The app will crash on while hiding the HUD,Did a research in stack overflow but couldn't found a solution for it.

1

There are 1 best solutions below

0
Mohammad Sadiq On

The error is quite descriptive. You are trying to set something nil that has already been release. Now you need to find the place where you are doing it and a way to avoid this. As you mentioned you are getting this while hiding it. So you have already zeroed on the issue.

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

Here. You are trying to to make mCallingView = nil. Remove this line and check if you get the error. Do you really need to make the source view nil, while hiding the activity indicator?