tags:

views:

174

answers:

2

Hi,

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

+2  A: 

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.

e.James
Many thanks. I see now I will need to go back through my code to address this. One other thing on this; to me self.pickList = P1 and pickList = P1 say the same thing. Can you think of a way I can see the two of these statements for what they mean? (if you understand what I mean :)
Chris
I tend to use the square bracket syntax whenever I'm setting a property value. The dot syntax may look a little cleaner, but I find it easier to remember that way. When I see var = value, I know I'm accessing the member directly. When I see [self setVar:value], I know that the accessors are being used.
e.James
Thanks for for explaining this and your time -Chris
Chris
No trouble. Good luck with your code :)
e.James
A: 

This was immensely helpful, I've bypassed the accessor methods so often with direct assignment.

Kirn