views:

259

answers:

2

Hi All,

I'm currently having a problem where the leaks tool is reporting a slew of memory leaks after clicking on cells within a UITableView and then hitting the back button and popping off the view.

Majority of the leaks reported can not be traced back to any specific location in my code, they are:

Leaked Object # Address Size Responsible Library Responsible Frame
NSCFArray 2 < multiple > 64 UIKit -[UITouch(UITouchInternal)
UITouch         2 < multiple > 128 GraphicsServices PurpleEventCallback
Malloc 48 Bytes 2 < multiple > 96 Foundation -[NSCFArray insertObject:atIndex:]
UIDelayedAction 2 < multiple > 96 UIKit -[UILongPressGestureRecognizer startTimer]
NSCFArray 2 < multiple > 64 UIKit -[UILongPressGestureRecognizer touchesBegan:withEvent:]
Malloc 32 Bytes 2 < multiple > 64 Foundation -[NSCFArray insertObject:atIndex:]
Malloc 16 Bytes 2 < multiple > 32 Foundation -[NSCFSet unionSet:]

Now I have commented out all my code in any touch event functions that I have written and it still leaks if I click on the cell a few times and then hit the back button to return to the previous view.

Any ideas on what might actually be the problem here?

Thanks,

EDIT: Including code

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {

  name = [[UILabel alloc] initWithFrame:CGRectMake(40, 18, 153, 21)];
  name.font = [UIFont boldSystemFontOfSize:14];
  name.textAlignment = UITextAlignmentCenter;
  name.backgroundColor = [UIColor clearColor];
  name.textColor = [UIColor blackColor];
  name.opaque = NO;
  [self.contentView addSubview:name];

  description = [[UILabel alloc] initWithFrame:CGRectMake(40, 35, 153, 21)];
  description.font = [UIFont boldSystemFontOfSize:14];
  description.textAlignment = UITextAlignmentCenter;
  description.backgroundColor = [UIColor clearColor];
  description.textColor = [UIColor blackColor];
  description.opaque = NO;
  [self.contentView addSubview:description];

  category = [[UILabel alloc] initWithFrame:CGRectMake(40, 1, 153, 21)];
  category.font = [UIFont boldSystemFontOfSize:12];
  category.textAlignment = UITextAlignmentCenter;
  category.backgroundColor = [UIColor clearColor];
  category.textColor = [UIColor blackColor];
  category.opaque = NO;
  [self.contentView addSubview:category];

  favorite = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
  CGRect favFrame = CGRectMake(5, 4, 42, 52);
  [favorite addTarget:self action:@selector(toggleFavoriteBtn:) forControlEvents:UIControlEventTouchUpInside];
  [favorite setImage:[g_imageCacher getCachedImage:@"favorite_star_empty.png"] forState:UIControlStateNormal];
  [favorite setImage:[g_imageCacher getCachedImage:@"favorite_star_selected.png"] forState:UIControlStateSelected];
  favorite.backgroundColor = [UIColor clearColor];
  favorite.frame = favFrame;
  [self.contentView addSubview:favorite];

  checkMark = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
  CGRect ckFrame = CGRectMake(201, 23, 32, 32);
  //[checkMark addTarget:self action:@selector(toggleFavoriteBtn:) forControlEvents:UIControlEventTouchUpInside];
  [checkMark setImage:[g_imageCacher getCachedImage:@"check_empty.png"] forState:UIControlStateNormal];
  [checkMark setImage:[g_imageCacher getCachedImage:@"check_filled.png"] forState:UIControlStateSelected];
  checkMark.backgroundColor = [UIColor clearColor];
  checkMark.frame = ckFrame;
  [self.contentView addSubview:checkMark];

  deleteButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
  CGRect deleteFrame = CGRectMake(183, 0, 64, 64);
  //[checkMark addTarget:self action:@selector(toggleFavoriteBtn:) forControlEvents:UIControlEventTouchUpInside];
  [deleteButton addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
  [deleteButton setImage:[g_imageCacher getCachedImage:@"delete_item.png"] forState:UIControlStateNormal];
  deleteButton.backgroundColor = [UIColor clearColor];
  deleteButton.frame = deleteFrame;
  deleteButton.hidden = YES;
  [self.contentView addSubview:deleteButton];

  couponView = [[UIImageView alloc] initWithFrame:CGRectMake(199, 4, 32, 16)];
  couponView.image = [g_imageCacher getCachedImage:@"coupon_small32x16.png"];
  couponView.backgroundColor = [UIColor clearColor];
  [self.contentView addSubview:couponView];

  self.selectionStyle = UITableViewCellSelectionStyleNone;

  ApplicationAppDelegate *appDelegate = (ApplicationAppDelegate *)[[UIApplication sharedApplication] delegate];
  itemMap = appDelegate.ItemMap;
  currentInsertIndexPaths = appDelegate.indexPaths;
  gItem = nil;
    }
    return self;
}

- (void)dealloc {
     [name release];
     [description release];
     [category release];
     [favorite release];
     [checkMark release];
     [couponView release];
     [deleteButton release];
    [super dealloc];
}
A: 

The area labled as a problem by leaks is where I alloc MyCustomCell

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    MyCustomCell *cell = (MyCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[MyCustomCell alloc] initWithFrame:CGRectMake(0, 0, 245, 64) reuseIdentifier:CellIdentifier] autorelease];
        cell.controller = self;
        cell.tableView = myTableView;
    }

And also this line afterwards:

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {

Other than that, it gives me no more information as to where the leak might be located.

Falcon
The leaks seem to be emanating from a UITextView object as it is the only object I know, that makes use UIDelayedAction, also would make use of UITouch.Post the initialization and dealloc code of MyCustomCell.
Kenny
I have edited the above post to include the init and dealloc functions of MyCustomCell.
Falcon
Please post the code in the question, rather than in answers.
eman
A: 

Well, I am about to call BS on Leaks because I have commented out 90% of all code within the table view and custom cell as well as remove my creation of the custom cell and made a plain old UITableViewCell.

The leak still happens if I press down on the cell for a few seconds and then press the back button on the view. Now Leaks tells me the leak has to deal with the if (cell == nil)......

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
Falcon