开发者

IPHONE UIIMAGEVIEW can't release?

开发者 https://www.devze.com 2023-02-05 22:55 出处:网络
guys.i\'m new to iphone. i encounter a problem.the uiimageview can\'t release...here is the code. #import <UIKit/UIKit.h>

guys.i'm new to iphone. i encounter a problem.the uiimageview can't release...here is the code.

    #import <UIKit/UIKit.h>
@interface imageNavView : UIImageView {

    int index;
}
@property int index;

@end
#import "imageNavView.h"


    @implementation imageNavView
    @synthesize index;
    - (id)initWithFrame:(CGRect)frameRect{
        self = [super initWithFrame:frameRect];
        if (self) {
            // Custom initialization.
            self.userInteractionEnabled=YES;
            self.multipleTouchEnabled=YES;
            self.opaque=YES;
            self.autoresizingMask=(UIViewAutoresizingFlexibleHeight|
                                   UIViewAutoresizingFlexibleWidth|
                                   UIViewAutoresizingFlexibleRightMargin
                                   |UIViewAutoresizingFlexibleLeftMargin
                                   |UIViewAutoresizingFlexibleTopMargin
                                   |UIViewAutoresizingFlexibleBottomMargin);
            index=0;
        }
        return self;
    }

    - (void)dealloc {
        NSLog(@"before image dealloc %开发者_Go百科i",[self retainCount]);-------- log is 1
            [super dealloc];
        NSLog(@"after image dealloc %i",[self retainCount]);-------- log is 1,why itn't bad access?
    }


    @end

why does the second log NSLog(@"after image dealloc %i",[self retainCount]); is 1,not a bad access,so the imageview is not released. i can't understand,any possible way can make this happen?...any advice will be gratefull.thanks in advance!


dealloc is a call-back method while system releases an object.

The following description is a api doc of dealloc,

Instead, an object’s dealloc method is invoked indirectly through the release NSObject protocol method (if the release message results in the receiver's retain count becoming 0).


This is an excellent example of exactly why you should

never call retainCount

. The absolute retain count of an object is an implementation detail and it will often be of a value that makes no sense.

The behavior of calling a method after something is deallocated is entirely undefined. In this case, it may crash, it may not. If you turned on zombie detection, it would definitely crash.

Since there is absolutely no way that retainCount could ever return 0, there is no reason for object deallocation to decrement the retain count to 0.

0

精彩评论

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