开发者

UIButton retain counter increase after touch

I creating set of objects same custom type. All objects have methods showDeleteButton had hideDeleteButton. I found that when I hide delete button (remove it) button which was pressed has retainCounter == 2.

Here the code:

-(void)showDeleteButton {
if(!isDeleteButtonLoaded) { // Check that method was't triggered twice
    UIButton *aDeleteButt开发者_StackOverflow社区on = [[UIButton alloc] initWithFrame:CGRectMake(-3, -7, 30, 29)]; // RC == 1
    [aDeleteButton setImage:[UIImage imageNamed:@"close_button.png"] forState:UIControlStateNormal];
    [self addSubview:aDeleteButton]; // RC == 2
    [aDeleteButton addTarget:self action:@selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    deleteButton = aDeleteButton;
    [aDeleteButton release]; // RC == 1
    isDeleteButtonLoaded = YES;
    NSLog(@"delete button retain count (created): %d", [deleteButton retainCount]);
    [self setNeedsDisplay];
}

}

-(void)deleteButtonPressed:(id)sender {
[delegate deleteImageAtPath:self.imageFullPath];

}

-(void)hideDeleteButton {
if(isDeleteButtonLoaded) {
    NSLog(@"delete button retain count (before): %d", [deleteButton retainCount]); // RC == 1
    [deleteButton removeFromSuperview]; // RC == 0
    deleteButton = nil;
    isDeleteButtonLoaded = NO;
    [self setNeedsDisplay];
}

}

So for pressed button NSLog in the second method displays RC == 2! Any ideas? I'm 100% sure that deleteButton isn't accessible from outside the class.


DO NOT LOOK AT RETAINCOUNT

ahem

retainCount is meaningless as far as any sane person is concerned, as long as you're following the memory rules things will work out fine.


Never look at the retainCounts for any useful information. Within Apple's inner framework, they may call retain or release on your objects at any time. All you need to worry about is that your retain (or alloc, or copy) and release statements are matched up correctly. Adding a view as a subview of another view (or adding it to any NSArray) will also increase its retainCount... you don't need to worry about that though.


See that stackoverflow popular question, about why never using retainCount and instead using Apple tools to track memory leaks.


Don't ever use retainCount. The results are misleading at best.

First order of business, go read Apple's memory management guidelines.

Second, use those guidelines to fix some memory issues in your code:

UIButton *aDeleteButton = [[UIButton alloc] initWithFrame:CGRectMake(-3, -7, 30, 29)];
// Retain count +1
// Some code...
deleteButton = aDeleteButton;
[aDeleteButton release];
// Retain count -1

You allocate a new UIButton and then you release it. This means the button is NOT owned by you and could disappear at any time. I'm guessing that you have a property in your class called deleteButton? If so, use it like this: self.deleteButton = aDeleteButton;

In your hideDeleteButton method, you probably want to use something that looks like this:

[self.deleteButton removeFromSuperview];
self.deleteButton = nil;
isDeleteButtonLoaded = NO;
[self setNeedsDisplay];

Again, this assumes that you have a property in your class named deleteButton. If you don't have one, make one and use it. It greatly simplifies memory management tasks.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜