Reputation: 31311
I'm trying to make app with ios5 and i'm really confused why happen this. Could someone help me an explain me why have error in lines when appear &error
Passing address of non-local object to __autoreleasing parameter for write-back
The below call cause the error
// note the following method returns _inStream and _outStream with a retain count that the caller must eventually release
if (![netService getInputStream:&_inStream outputStream:&_outStream]) {
NSLog(@"error in get input and output streams");
return;
}
my class .h
NSInputStream *_inStream;
NSOutputStream *_outStream;
@property (nonatomic, strong) NSInputStream *_inStream;
@property (nonatomic, strong) NSOutputStream *_outStream;
my class .m
@synthesize _inStream;
@synthesize _outStream;
The getInputStream is a NSNetService class method
Please the NSNetService class getInputStream implementation below
/* Retrieves streams from the NSNetService instance. The instance's delegate methods are not called. Returns YES if the streams requested are created successfully. Returns NO if or any reason the stream could not be created. If only one stream is desired, pass NULL for the address of the other stream. The streams that are created are not open, and are not scheduled in any run loop for any mode.
*/
- (BOOL)getInputStream:(NSInputStream **)inputStream outputStream:(NSOutputStream **)outputStream;
I have found the related posts in
non-local-object-to-autoreleasing-parameter
non-local-object-to-autoreleasing
from-ios-4-to-ios-5arc-passing-address
However i'm unable to found the issue
Any help on this issue is appreciated
Thanks
Upvotes: 2
Views: 4197
Reputation: 31311
I have found the solution
Here we go. Create the variables as local and assign them back to the original. Thanks to @borreden for the insightful info.
Here is my latest code.
NSInputStream *tempInput = nil;
NSOutputStream *tempOutput = nil;
// note the following method returns _inStream and _outStream with a retain count that the caller must eventually release
if (![netService getInputStream:&tempInput outputStream:&tempOutput]) {
NSLog(@"error in get input and output streams");
return;
}
_inStream = tempInput;
_outStream = tempOutput;
Upvotes: 6
Reputation: 11053
The reason is that you can't pass your _inStream
and _outStream
like this when using ARC. (since you are using ARC in the converted version)
Since if these variables are overwritten within the method they would leak.
You need to change you handle the return value (if it changes at all) or just pass as a normal parameter.
There is no explicit memory management with retain/release anymore so ARC can not handle this case automatically.
Upvotes: 0