开发者

NSMutableArray getting released?

开发者 https://www.devze.com 2023-03-06 11:26 出处:网络
I have a NSMutableArray whose pr开发者_如何学Gooperty is (nonatomic, retain), and it\'s getting released for some reason, here\'s the code when I assign it (tableData is the NSMutableArray):

I have a NSMutableArray whose pr开发者_如何学Gooperty is (nonatomic, retain), and it's getting released for some reason, here's the code when I assign it (tableData is the NSMutableArray):

tableData = [[[NSMutableArray alloc] init] autorelease];

When I alloc it, it's retain count is 1. When I assign it to tableData, it's retain count is 2. Then with auto release, at the end of runloop it should go to 1 (which is what I want). But this doesn't happen. Later on, when I reference it and run a method, I get random objects (last time it was UITabBarSwappableImageView) and it says unrecognized selector sent. Which makes sense...the object is being released and some other object is taking it's place...But why is it being released? Thanks.


It's getting released because assigning it to tableData does not alter the retain count.

You're simply copying a pointer value from one slot in memory to another slot. That's it. No message is dispatched to indicated a change in ownership.

You need to actually invoke the setter method:

[self setTableData:[NSMutableArray array]];

Or if you prefer dot syntax:

self.tableData = [NSMutableArray array];


Did you use the dot syntax? If not, the synthesized setter is not used thus the reference is not retained.

It should be assigned like this:

self.tableData = [[[NSMutableArray alloc] init] autorelease];
0

精彩评论

暂无评论...
验证码 换一张
取 消