iOS中内存管理的问题(创建一个对象后他的引用计数为什么是3)

chenyu 发布于 2013年10月05日
无人欣赏。

在两个个viewController之间传递数据,FirstViewController有一个button,点击后跳转到SecondViewController,点击SecondViewController界面上的button,跳回第一个界面,并修改第一个界面中button上的文字。 目前程序能做出来,但是遇到了一些疑惑,就是程序运行起来后打印了FirstViewController界面对象的引用计数,打印出来为什么是3,在SecondViewController.h文件中使用retain定义了FirstViewController的属性,为什么在第二个界面打印第一个界面对象的引用计数依然是3. @tinyfool @kxdd2002 @清醒疯子 能给解答一下吗?或者告诉我在哪里可以找到有关于这方面的资料?多谢啦 代码地址:http://pan.baidu.com/s/1eMfNl

代码:第一个界面的.h和.m文件

#import 
#import "SecondViewController.h"
@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UIButton *button;

- (IBAction)myButton:(id)sender;
-(void)showNextPage;
-(void)changeButtonTitle:(NSString *)str;
@end

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //次数打印当前对象的引用计数为什么是3 ???????????????????????
    NSLog(@"self retaincount: %d",[self retainCount]);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_button release];
    [super dealloc];
}
- (IBAction)myButton:(id)sender {
    
    [self showNextPage];
}

-(void)showNextPage
{
    SecondViewController * secondController = [[SecondViewController alloc]init];
    secondController.first = self;
    [self presentViewController:secondController animated:YES completion:nil];
}

-(void)changeButtonTitle:(NSString *)str
{
    NSLog(@"buttonTitle: %@",str);
    self.button.titleLabel.text = str;
}
@end

第二个界面的.h和.m


#import 
@class ViewController;
@interface SecondViewController : UIViewController
//此处使用retain好还是用assign好  ???????????????????????
@property (nonatomic,retain)ViewController * first;

- (IBAction)back:(id)sender;
@end

#import "SecondViewController.h"
#import "ViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)back:(id)sender
{
  //在.h文件中声明first的时候用了retain,此处打印first的引用计数为什么还是3,不应该是4吗??????????????????????? 
    NSLog(@"first retaincount: %d",[self.first retainCount]);
    [self.first changeButtonTitle:@"frist"];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
@end
共4条回复
tinyfool 回复于 2013年10月05日

@sycx 老师接客

sycx 回复于 2013年10月05日

你应该看看文档,读取retainCount的值没啥意义,framework本身会按需要retain你的object,你很难保证它们什么会retain以及会retain几次;

你的代码里:

  1. viewDidLoad时retainCount为什么是3 -> framework retain了
  2. back:时为什么还是3 -> framework 刚巧release 了一次
  3. SecondViewController里,firstassign比较好

PS: 如果你真的很有兴趣知道retainCount为什么是3, 你可以重载retain函数,然后下个断点看看

chenyu 回复于 2013年10月05日

2楼 @sycx 很满意,五星………………

phoenix5980 回复于 2013年10月05日

2楼 @sycx 老师已经是超神的节奏了么(。ÒㅅÓ)ノ

登录 或者 注册
相关帖子