2011-01-22 73 views
0

guys.i'm new to iphone。 我遇到一个问题。uiimageview无法发布...这里是代码。IPHONE UIIMAGEVIEW无法发布?

#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 %i",[self retainCount]);-------- log is 1 
      [super dealloc]; 
     NSLog(@"after image dealloc %i",[self retainCount]);-------- log is 1,why itn't bad access? 
    } 


    @end 

为什么第二次登录NSLog(@“after image dealloc%i”,[self retainCount]);是1,访问不错,所以imageview不会被释放。 我不明白,任何可能的方式可以做到这一点?...任何意见将gratefull.thanks提前!

回答

0

dealloc是系统释放对象时的回调方法。

下面的描述是的dealloc的API文档,

相反,一个对象的dealloc方法是间接地通过释放NSObject的协议方法调用的(如果在接收机的保留计数所述释放消息的结果成为0)。

0

这是一个很好的例子,你应该为什么

从来没有打电话给retainCount

。对象的绝对保留数是一个实现细节,它通常是一个没有意义的值。

解除分配后调用方法的行为是完全未定义。在这种情况下,它可能会崩溃,它可能不会。如果你打开僵尸检测,它肯定会崩溃。

既然是绝对没有办法,retainCount所能返回0,没有理由为对象释放递减保留计数为0

+0

我觉得编译器甚至应该发出警告调用它。 – 2011-01-22 21:06:57