AFNetworking 2启用ARC后异步内存泄漏

时间:2014-02-04 07:08:52

标签: ios objective-c memory-leaks automatic-ref-counting afnetworking-2

我对iOS编码很新。我从以前的程序员那里继承了一段代码,将代码分成几个部分是个好主意。我希望我能说明这一点。

postservice.h的一部分

@property(nonatomic)SEL ReturnAction;
@property(nonatomic)id  ReturnDelegate;
postservice.m中调用的

方法

-(void)Send:(NSString *)Pmethod  Mparams:(NSDictionary *)Mparams Mdelegate:(id)Mdelegate IsSycn:(BOOL)IsSycn ExternalUrl:(BOOL)ExternalUrl{
Murl=[Murl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:Murl]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager POST:Murl parameters:Mparams success:^(AFHTTPRequestOperation *operation, id responseObject) { 
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        //NSLog(@"success %@", string);
            if ([ReturnDelegate respondsToSelector:ReturnAction]) {
                ((void (*)(id, SEL, id))[ReturnDelegate methodForSelector:ReturnAction])(ReturnDelegate, ReturnAction, string);
        }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            //NSLog(@"error %@", error);
            if ([ReturnDelegate respondsToSelector:ReturnAction]) {
                ((void (*)(id, SEL, id))[ReturnDelegate methodForSelector:ReturnAction])(ReturnDelegate, ReturnAction, error);
            }
          }];
}

然后从另一个中级类方法调用它,

SE_MidPostService.h的一部分

@property(nonatomic)SEL FetchCompleted;
@property(nonatomic)SEL FetchFailed;
@property(nonatomic)id  ReturnDelegate;//unsafe_unretained
@property(nonatomic, retain)PostService * service;

SE_MidPostService.m的示例部分

  -(void)GetDeviceStatus:(NSString *)DeviceID RestID:(NSNumber *)ReID
{
    NSLog(@"GetDeviceStatus");
    NSDictionary * Parameters=[[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:DeviceID,ReID, @"GetStat", nil]
                                                             forKeys:[NSArray arrayWithObjects:@"DeviceUniqueId",@"ReID", @"OP",nil]];
    [service setReturnAction:@selector(GetDeviceStatusCallBack:)];
    [service setReturnDelegate:self];
    [service Send:@"GetStat" Mparams:Parameters Mdelegate:nil IsSycn:NO ExternalUrl:NO];

}
-(void)GetDeviceStatusCallBack:(id)value{
    NSLog(@"GetDeviceStatusCallBack");
    if([value isKindOfClass:[NSError class]]) {
        if ([ReturnDelegate respondsToSelector:FetchCompleted]) {
           ((void (*)(id, SEL, id, id))[ReturnDelegate methodForSelector:FetchCompleted])(ReturnDelegate, FetchCompleted, nil, value);
        }
        return;
    }
    if ([ReturnDelegate respondsToSelector:FetchCompleted]) {
        ((void (*)(id, SEL, id, id))[ReturnDelegate methodForSelector:FetchCompleted])(ReturnDelegate, FetchCompleted, value, nil);
    }
}

这些被代码的不同部分调用,这简化了代码,

    SE_MidPostService * service=[[SE_MidPostService alloc] init];
    [service setReturnDelegate:self];
    [service setFetchCompleted:@selector(DeviceStatusCheck:err:)];
    [service GetDeviceStatus:uuid RestID:id;
}

-(void)DeviceStatusCheck:(NSNumber *)Result err:(NSError *)err{
    if (err!=nil) {...}
//does what ever is needed
}

乐器主要突出显示返回的代码。

((void (*)(id, SEL, id, id))[ReturnDelegate methodForSelector:FetchCompleted])(ReturnDelegate, FetchCompleted, value, nil);

我认为ARC会照顾这个,但事实并非如此。 我确实读过一些关于autoreleasepool的内容。我是否需要在成功编码中添加此内容。

@autoreleasepool {
    // Code that creates autoreleased objects.
}

我希望我在这里有足够的信息。很抱歉在iOS上是新手,没有帮助。

2 个答案:

答案 0 :(得分:1)

如果其他人想要我解决问题的方法是,我确实一起摆脱了子类。

这确实在编码方面增加了额外的工作,但它不会有任何泄漏。

这是我使用的代码。我还看了wwdc 2013内存管理会话。很有帮助。

NSString * Murl;
        Murl = [NSString stringWithFormat:@"http://%@/service.aspx",[[GlobalData sharedGlobalData].settings ServerIP]];
        Murl=[Murl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:Murl]];
        __weak typeof(self) weakmyself = self;
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        [manager POST:Murl parameters:Parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            NSNumber *num = [NSNumber numberWithInteger: [string integerValue]];
            @autoreleasepool{
                //NSLog(@"success %@", string);
                [weakmyself Callbackfunction:num err:nil];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            //NSLog(@"error %@", error);
            NSError * err = error;
            @autoreleasepool{
                [weakmyself Callbackfunction:nil err:err];

            }
        }];

答案 1 :(得分:0)

以这种方式调用方法时:

 ((void (*)(id, SEL, id, id))[ReturnDelegate methodForSelector:FetchCompleted])(ReturnDelegate, FetchCompleted, nil, value);

ARC没有任何合理插入保留和释放的方法,因此可以理解这会导致泄漏或崩溃。

我在其他地方已经读过这个,除了切换到块而不是委托之外,似乎没有一个好的解决方案,无论如何这通常更干净,更灵活。

只需在你想要执行的块中传递你的委托传递而不是选择器。