1

The code below works fine, I just don't know where the release should go, because I'm not sure what the rules are. I'm not using ARC.

- (void)myFunc {

    // stuff happens

    __block UIImage* photo = [UIImage imageWithCGImage:croppedCGImage];

    [photo retain];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
                                         (unsigned long)NULL), ^(void) {
        [self doStuffToPhoto:photo];
        // [photo release] causes EXC_BAD_ACCESS
    });

    // [photo release] causes EXC_BAD_ACCESS in doStuffToPhoto
}

- (void)doStuffToPhoto:(UIImage*)photo {
    // do stuff
    // [photo release] causes EXC_BAD_ACCESS
} 
Curyous
  • 8,716
  • 15
  • 58
  • 83
  • p.s. there is no reason to use `__block` on `photo`, as `photo` is not assigned to anywhere – newacct May 08 '13 at 10:23
  • putting `[photo release]` at the end of the inside of the `dispatch_async` block should not cause any problems. Your problem must be caused by something you are not showing. – newacct May 08 '13 at 20:07

1 Answers1

0

If I understand the docs right (look for The block Storage Type and Object and Block Variables here), there is no need to retain your photo variable: "__block variables live in storage that is shared between the lexical scope of the variable and all blocks and block copies declared or created within the variable’s lexical scope. Thus, the storage will survive the destruction of the stack frame if any copies of the blocks declared within the frame survive beyond the end of the frame".
But this no explanation why you get the EXC_BAD_ACCESS.

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
  • You missed the point. The *variable* (a pointer) lives on the shared storage. The *object it points to* needs to be retained and released. – newacct May 08 '13 at 10:22
  • @newacct: Thanks for pointing this out. Anyway, I was not completely sure ("If I understand the docs right..."). – Reinhard Männer May 08 '13 at 11:35