码迷,mamicode.com
首页 > 其他好文 > 详细

Objc中2维指针作为输出参数时由ARC及@autoreleasepool引发的血案

时间:2014-07-23 16:44:01      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   strong   io   re   

先看下面一个例子

#import <UIKit/UIKit.h>

#import "AppDelegate.h"


@interface Something : NSObject

- (void)doWithError:(NSError **)outError;

@end

@implementation Something

- (void)doWithError:(NSError **)outError
{
    @autoreleasepool
    {
        *outError = [NSError errorWithDomain:@"Emergency" code:999 userInfo:nil];
    }
}

@end

int main(int argc, const char *argv[])
{
    @autoreleasepool
    {
        NSError *error = nil;

     // Sometimes EXC_BAD_ACCESS when return from this method. [[[Something alloc] init] doWithError:&error]; // At this point, the main thread gives EXC_BAD_ACCESS. NSLog(@"%@", error); } return 0; }

 在NSLog输出的时候会EXC_Bad_ACCESS

  

正确写法应该是这样:

 

@interface Something : NSObject

- (void)doWithError:(NSError **)outError;

@end

@implementation Something

- (void)doWithError:(NSError **)outError
{
    NSError *error = nil;
    @autoreleasepool
    {
        error = [NSError errorWithDomain:@"Emergency" code:999 userInfo:nil];
        // Do something
    }
    if(error)
    {
        *outError = error;
    }
}

@end

int main(int argc, char * argv[])
{
    @autoreleasepool
    {
        NSError *__autoreleasing error = nil;
        
        [[[Something alloc] init] doWithError:&error];
        
        // At this point, the main thread gives EXC_BAD_ACCESS.
        NSLog(@"%@", error);
        
        int a = 1;
        ++a;
    }
    return 0;
}

 

 

 

Objc中2维指针作为输出参数时由ARC及@autoreleasepool引发的血案,布布扣,bubuko.com

Objc中2维指针作为输出参数时由ARC及@autoreleasepool引发的血案

标签:style   blog   color   strong   io   re   

原文地址:http://www.cnblogs.com/csutanyu/p/3782953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!