“-[CFString respondsToSelector:]: message sent to deallocated instance” when trying to access objectArray

匿名 (未验证) 提交于 2019-12-03 10:10:24

问题:

I am trying to load next 10 rows of data in tableView when program fetches the last row in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

I am using single function with pagenumber parameter to load the consecutive pages. It works for first fetch i.e. pagenumber 0, but when I call it again to fetch next page rows, it gives me this error

-[CFString respondsToSelector:]: message sent to deallocated instance 

After banging my head for a long time, I've found that it creates this problem when trying to access the NSMutableArray of my Model objects. I am using it this way :

TableRowObjectClass* TempObject = [[TableRowObject alloc] init]; for(int i=0;i<rowArray.count;i++){     TempObject = [rowArray objectAtIndex:i];     NSString* objectProperty = [[[NSString alloc] initWithFormat:@"String Property I need from Object to be appended with some text from TableRowObject Class : %@",TempObject.objProperty] retain];     [propertyArray addObject:objectProperty];     [objectProperty release]; } 

Here I get array of Model Objects(TableRowObjectClass's Objects) and I want to extract objectProperty from TempObject and create and array of all those properties. Initially, for pagenumber 0, it fetches 20 rows, now when I am displaying 20th row I call fetch, which call this function to create a fresh Array of objectProperty and I call [TableView reloadData] to show the frsh feed with 20(old)+20(fresh) feeds.

Now its creating this error

-[CFString respondsToSelector:]: message sent to deallocated instance 

when trying to access,

NSString* objectProperty = [[[NSString alloc] initWithFormat:@"String Property ....TableRowObject Class: %@",TempObject.objProperty] retain]; 

I am not sure what and where is it getting dealloc. I've spent alot of hours on this and I am not really good with objective-c memory management.

Your help is highly appreciated.

回答1:

Your code pretty clearly indicates that you don't understand Objective-C or Objective-C memory management; no worries, we've all started there. Start by reading the Objective-C guide, then the Memory Management guide.

Some specific issues:

TableRowObjectClass* TempObject = [[TableRowObject alloc] init]; TempObject = [someArray objectAtIndex: 0]; 

That leaks. the object allocated on the first line (the variable should start with a lower case letter, too).

This:

NSString* objectProperty = [[[NSString alloc] initWithFormat:@"..."] retain]; [objectProperty release]; 

A double retain balanced by a single release, another leak.

That you are seeing message sent to deallocated instance errors indicates that you are over-releaseing other objects. Follow the memory management guidelines, use "build and analyze", and then use the Zombie detection tool....



回答2:

There is no necessity to use

NSString* objectProperty = [[[NSString alloc] initWithFormat:@"String Property ....TableRowObject : %@",TableRowObject.objProperty] retain]; 

you can even use

NSString* objectProperty = [NSString stringWithFormat:@"String Property ....TableRowObject : %@",TableRowObject.objProperty]; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!