I'm having trouble implementing the NSFastEnumeration protocol while migrating Objective-C code to ARC. Could someone tell me, how to get rid of the following warnig (see code snippet)? Thanks in advance.
// I changed it due to ARC, was before
// - (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*) state objects: (id*) stackbuf count: (NSUInteger) len
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*) state objects: (__unsafe_unretained id *) stackbuf count: (NSUInteger) len
{
...
*stackbuf = [[ZBarSymbol alloc] initWithSymbol: sym]; //Warning: Assigning retained object to unsafe_unretained variable; object will be released after assignment
...
}
- (id) initWithSymbol: (const zbar_symbol_t*) sym
{
if(self = [super init]) {
...
}
return(self);
}
With ARC, something has to hold a strong or autoreleasing reference to each object, otherwise it will be released (just as the warning says). Because stackbuf is __unsafe_unretained, it's not going to hang onto the ZBarSymbol for you.
If you create a temporary autoreleasing variable and stash your object there, it will live until the current autorelease pool is popped. You can then point stackbuf to it without complaint.
ZBarSymbol * __autoreleasing tmp = [[ZBarSymbol alloc] initWithSymbol: sym];
*stackbuf = tmp;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With