Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error "Passing address of non-local object to __autoreleasing parameter for write-back"

I'm converting my socket client to ARC:

- (id)initWithHostname:(NSString *)hostname AndPort:(NSInteger)port {     if((self = [super init]))     {         oBuffer = [[NSMutableData alloc] init];         iBuffer = [[NSMutableData alloc] init];          iStream = [[NSInputStream alloc] init];         oStream = [[NSOutputStream alloc] init];          [NSStream getStreamsToHost:[NSHost hostWithName:hostname] port:port inputStream:&iStream outputStream:&oStream];          ...     }      return self; } 

The error I got is:

error: Automatic Reference Counting Issue: Passing address of non-local object to __autoreleasing parameter for write-back

at this line on &iStream and &oStream:

[NSStream getStreamsToHost:[NSHost hostWithName:hostname] port:port inputStream:&iStream outputStream:&oStream]; 

Any help?

like image 446
kilianc Avatar asked Aug 05 '11 23:08

kilianc


2 Answers

This error is usually due to the non local variable address is passed to a method. Because the variable is declared as __strong by default, while the parameter of the method is __autoreleasing, so declare the parameter of the method invoked as __strong,like this: -(void)method:(id * __strong *)param.

Note that the method in the header file (.h file) must be declared as the same of the .m file.

like image 121
慭慭流觞 Avatar answered Oct 12 '22 09:10

慭慭流觞


Create two local variables, pass the addresses of them to the the method, then assign their values to the ivars after it returns.

like image 35
Mann Avatar answered Oct 12 '22 11:10

Mann