Skip to content

Instantly share code, notes, and snippets.

@stigi
Created July 30, 2014 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stigi/11cccd6778ebd83aae49 to your computer and use it in GitHub Desktop.
Save stigi/11cccd6778ebd83aae49 to your computer and use it in GitHub Desktop.
Bad idea
MyCrazyBlock block = ^(BOOL *stop) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// do some work
dispatch_async(dispatch_get_main_queue(), ^{
*stop = YES;
});
});
};
// in your object
if (block) block(&self.shouldStop);
@stigi
Copy link
Author

stigi commented Jul 30, 2014

When your object is released and deallocated (might happen during "some work") the pointer to the stop BOOL is still held but points to invalid (maybe even some other objects) memory. When you write to its location you might end up writing to some other objects memory.

Result: Funky crashes (EXC_BAD_ACCESS & SIGSEGV) all over the place.

@hiDominik
Copy link

just a wild guess, but what if you call the block with a weak pointer to self?

__weak typeof(self) weakSelf = self;
if (block) block(&weakSelf.shouldStop);

@stigi
Copy link
Author

stigi commented Jul 30, 2014

Please ignore the fact that setting the stop BOOL asynchronously never worked anyway ;)

@stigi
Copy link
Author

stigi commented Jul 30, 2014

@hiDominik: should not make any difference, as ARC only manages object retain counts. we're dealing with a pointer into an objects memory space here. Nothing that could be retained.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment