views:

23

answers:

1

Hi Guys,

I am getting allocation count plus one in the below code.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MasterViewIdentifier"] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        UIView* elementView =  [ [UIView alloc] initWithFrame:CGRectMake(5,5,312,480)];
        elementView.tag = 0;
        [cell.contentView addSubview:elementView];
        [elementView release];
    }
    UIView* elementView  = [cell.contentView viewWithTag:0];
    for(UIView* subView in elementView.subviews)
    {
        [subView removeFromSuperview];
    }

    if(indexPath.section==0)
    {
        if(indexPath.row==0)
        {
                   // i have tried like this but getting exception.
            /*
            UIImage *cellBackImag=[UIImage imageNamed:@"celltop.png" ];
            UIImageView* imaeView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 43)];
            imaeView.image=cellBackImag;
            [elementView addSubview:imaeView];
            [imaeView release];
            [cellBackImag release];
             */


            // here I am getting uiimage count allocation count 
            UIImageView* imaeView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"celltop.png" ]];
            imaeView.frame=CGRectMake(0, 0, 300, 43);
            [elementView addSubview:imaeView];
            [imaeView release];

Please help me out of this, Thank You, Madan Mohan

+2  A: 

First of all you should not rely on the retain count, because when you pass the object to some method all things might happen with this object, like someone retains it, autoreleases it again, passes it to another method, etc, etc. So the value is highly inaccurate.

But here in your case, the retain count of (at least) +1 is correct, because you want your image view to remain alive after you added it as a subview to elementView, so [element addSubview:imageView] retains your image view (adding +1 to the retain count) and then you release it again (leaving the +1 from alloc init).

The reason why you get the exception with the commented out code is that you create an autoreleased object for cellBackImag and then you release it. After your method returns, at some point the autorelease pool will release the image again, and there it will crash.

So basically just stick with the second approach (alloc, init, add, release) and forget about the retain count. If you're worried about memory leaks, you should take a look at Instruments, which is a great tool for finding all kinds of bugs.

frenetisch applaudierend
+1. Ignore the actual count, use proper memory management (i.e if you own it, you're responsible for releasing it) and use Instruments to find actual leaks.
Abizern