即使属性设置为保留,也会出现BAD ACCESS错误 [英] BAD ACCESS error even though property is set to retain

查看:96
本文介绍了即使属性设置为保留,也会出现BAD ACCESS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人能告诉我为什么我错了。以下是我的代码片段:

  TimeLogAppDelegate * appDelegate =(TimeLogAppDelegate *)[[UIApplication sharedApplication] delegate]; 

PickFromListViewController * pl = [[PickFromListViewController alloc] initWithNibName:@PickListbundle:nil];
pickList = pl;
[pickList setSearchItems:[appDelegate tableListMutableArray:type]:NSLocalizedString(type,nil)];
pickList.callingViewController = self;
[pl release];
pickList.responseSelector = [[type lowercaseString] stringByAppendingString:@Selected:];
pickList.includeNone = YES;
pickList.includeNew = YES;
[self.navigationController pushViewController:pickList animated:YES];

正如你所看到的,我正在发布一半,只是为了解决这个问题。 'pickList'显然是一个PickFromListViewController并在Header中声明。我将其设置为属性(@property(非原子,保留)PickFromListViewController * pickList;)并且我@synthesize它。



我的问题是:

$在释放pl之后,我得到一个BAD ACCESS错误,访问pickList,指示指针不再可用,但我认为pickList是合成的,它会被保留,直到我发布它为止dealloc?



有人可以告诉我为什么我错了吗?



非常感谢

解决方案

pickList 属性设置为(非原子,保留)仅在您通过属性访问者访问该属性时才适用,如下所示:

  self.pickList = P1; 

  [self setPickList:P1]; 

如果您只是为成员变量赋值:

  pickList = P1; 

您绕过属性访问器方法,因此没有保留消息已发送。


I hope someone can tell me why I'm wrong. Here is a snippet of my code:

TimeLogAppDelegate *appDelegate = (TimeLogAppDelegate *)[[UIApplication sharedApplication] delegate];

PickFromListViewController * pl = [[PickFromListViewController alloc] initWithNibName:@"PickList" bundle:nil];
pickList = pl;
[pickList setSearchItems:[appDelegate tableListMutableArray:type ] :NSLocalizedString(type,nil)];
pickList.callingViewController = self;
[pl release];
pickList.responseSelector = [[type lowercaseString] stringByAppendingString: @"Selected:"];
pickList.includeNone = YES;
pickList.includeNew = YES;
[self.navigationController pushViewController:pickList animated:YES];

As you can see, I am releasing pl half-way through, just to create the problem. 'pickList' is obviously a PickFromListViewController and is declared in the Header. I set it up as a property (@property (nonatomic, retain) PickFromListViewController *pickList;) and I @synthesize it.

My problem is:

after pl is release I get a BAD ACCESS error accessing pickList indicating that the pointer is no longer available, but I thought the fact that pickList is synthesized, it would be retained until I release it at dealloc?

Can someone tell me why I am wrong?

Many thanks

解决方案

Setting up your pickList property as (nonatomic, retain) only applies if you access the property via a property accessor, as follows:

self.pickList = P1;

or

[self setPickList:P1];

If you simply assign a value to the member variable:

pickList = P1;

You are bypassing the property accessor methods, so no retain message is sent.

这篇关于即使属性设置为保留,也会出现BAD ACCESS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆