Andrew
Andrew

Reputation: 16051

Crash when object deallocated while running a background thread

I have a set of objects, init'd and owned by a view controller. The view controller starts a fetchData method in each of these objects, which runs in a background thread and then, when it's done, returns the main thread and changes a property - self.dataFetchComplete, which the view controller is observing.

dispatch_async(dispatch_get_main_queue(), ^{        
    self.dataFetchComplete = YES;
});

If the view controller is deallocated before these objects are finished with this method, when it hits the self.dataFetchComplete = YES; line, it crashes.

How can i stop it from crashing when trying to change this property after being deallocated.

EDIT: More info, and clarity.

Upvotes: 3

Views: 393

Answers (1)

bbum
bbum

Reputation: 162712

That block has a strong reference to self. The only way self could be deallocated is if you are over-releasing it elsewhere (as @Catfish_Man indicated).

  1. if you have a crash, post the backtrace of the crash

  2. if you have an over-release crash, use Instruments to track all retain/release events on the object.

  3. run the analyzer and fix any problems it indicates.

Upvotes: 1

Related Questions