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

后端 未结 3 1994
别跟我提以往
别跟我提以往 2020-12-29 03:22

I\'m converting my socket client to ARC:

- (id)initWithHostname:(NSString *)hostname AndPort:(NSInteger)port
{
    if((self = [super init]))
    {
        oB         


        
相关标签:
3条回答
  • 2020-12-29 04:02

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

    0 讨论(0)
  • 2020-12-29 04:03

    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.

    0 讨论(0)
  • 2020-12-29 04:04

    I think you should not alloc and init the iStream and oStream variables. They are meant to receive. Without ARC this simply creates two memory leaks that go unnoticed. Now your compiler uses ARC and then it does matter. The receiving variables should be local:

    So try:

        NSInputStream *iStream;
        NSOutputStream *oStream;
    
        oBuffer = [[NSMutableData alloc] init];
        iBuffer = [[NSMutableData alloc] init];
    
        [NSStream getStreamsToHost:[NSHost hostWithName:hostname] port:port inputStream:&iStream outputStream:&oStream];
    

    That should work, AFAICT. But note: I can't test this here.

    0 讨论(0)
提交回复
热议问题