user1157838
user1157838

Reputation: 131

retaincount will be increased after copy an array

I am developing one application. In that I am performing search operation once. see my search method:

-(void)searchData
{
//[A release];
B =[A copy];
[A release];

NSLog(@"A retain count %i",[A retainCount]);
 NSLog(@"B retain count %i",[B retainCount]);
C = [[NSMutableArray alloc]init];
[C removeAllObjects];
for (int i=0; i<[B count]; i++) {
    DataBaseFields *data = [B objectAtIndex:i];

    NSRange range=  [data.DBtitle rangeOfString:searchEvents.text options:NSCaseInsensitiveSearch];
        if (range.length>0) {
        [C addObject:data];

    }

}
NSLog(@"shouldChangeTextInRange text is %@",searchEvents.text);
A = [C copy];

NSLog(@"A retain count %i",[A retainCount]);
NSLog(@"C retain count %i",[C retainCount]);
[tableView1 reloadData];
}

In this code A,B,C are three NSMutableArrays. I am not allocating the memory and didn't write properties for that arrays. In this if search operation display any data then Array A shows the retainCount at last 1. If there is no result then Array A shows the retainCount at last 32 or 46. So please tell me how to solve this one.

Upvotes: 0

Views: 129

Answers (1)

bbum
bbum

Reputation: 162712

retainCount is useless. Don't call it.

Beyond being generically meaningless, you are seeing odd retain counts in this code because, as an implementation detail, the frameworks are likely calling retain/autorelease on the objects. Since the retain count doesn't reflect the # of times an object has been autoreleased, the retain count is extra meaningless in your code.

Note also that the removeAllObjects in this is unnecessary.

C = [[NSMutableArray alloc]init];
[C removeAllObjects];

Better yet: Since it appears that you are relatively new to iOS development, turn on ARC and be done with all those retain/release/autorelease calls entirely.

Upvotes: 4

Related Questions