0

我有一个在启动新视图控制器时设置的属性。

- (IBAction)headerPressed:(UIButton *)sender {
    RouteViewController *route = [[RouteViewController alloc] initWithRoute:[[Route getRoute:TEST_ROUTE] autorelease]];
    [[self navigationController] pushViewController:circle animated:NO];
}

在新视图中,这是一个 UIViewController

RouteViewController.h

@interface =RouteViewController : UIViewController <MKMapViewDelegate> {
    Route *r;
}

-(id)initWithRoute:(Route *)route;


@property (retain, nonatomic) Route *r;

路由视图控制器.m

-(id)initWithRoute:(Route *)route{
    self = [super init];

    r = route;

    return self;
}
- (void)loadView {
    [super loadView];

    NSLog(@"TEST: %d", r.Route);  // throws exception. Actually ANY time I access my r property

}

r.Route 是 constTEST_ROUTE包含的 int 值。

现在,当我设置断点时,在 NSLog 时,检查器显示我的对象包含

Self >
    r >
       Route: 1

r >
    Route: 1

所以该属性是可用的,并且它确实包含正确的数据。但是当我尝试访问该属性时,它会抛出一个 EXC_BAD_ACCESS。

想法?:) (这在杀死我!)

4

2 回答 2

2

看起来您没有使用 ARC,但您也没有在 init 方法中保留路由。改变这个:

r = route;

对此:

r = [route retain];
于 2012-11-19T18:25:40.283 回答
2

这里有几个问题:

RouteViewController *route = [[RouteViewController alloc] initWithRoute:[[Route getRoute:TEST_ROUTE] autorelease]];

我认为自动释放消息应该发送到RouteViewController这里而不是Route对象,因为getRoute由于命名约定,该方法应该返回一个自动释放的对象。

IE

RouteViewController *route = [[[RouteViewController alloc] initWithRoute:[Route getRoute:TEST_ROUTE]] autorelease];

您需要保留传递给initWithRoute方法的 Route 对象的所有权:

任何一个:

-(id)initWithRoute:(Route *)route{
    self = [super init];

    r = [route retain];

    return self;
}

或者:

-(id)initWithRoute:(Route *)route{
    self = [super init];

    self.r = route;

    return self;
}
于 2012-11-19T18:27:01.377 回答