2011-07-15 33 views
0

我的很多内存泄露来自识别滑动的代码。我究竟做错了什么?第一行是我认为泄漏的东西(使用仪器)。它被示为很多的错误负责主叫 这是在viewDidLoad中:使用手势识别器进行内存管理

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)]; 
    [(UISwipeGestureRecognizer *)swipeRight setNumberOfTouchesRequired:2]; 

    swipeRight.direction = UISwipeGestureRecognizerDirectionRight; 
    swipeRight.delegate = self; 
    [webView addGestureRecognizer:swipeRight]; 

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)]; 
    [(UISwipeGestureRecognizer *)swipeLeft setNumberOfTouchesRequired:2]; 
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; 
    swipeLeft.delegate = self; 
    [webView addGestureRecognizer:swipeLeft]; 

    // Do any additional setup after loading the view from its nib. 

} 

还有一个问题,是什么在这里可以导致一个僵尸?我应该自动回收?

AViewController *a = [[AViewController alloc]init]; 
[self.navigationController pushViewController:a animated:YES]; 

a.title [email protected]"A View"; 
[a release]; 

更新3:我跑的仪器寻找坏的分配,并与一些集约利用我这里得到僵尸: 错误消息:An Objective-C message was sent to a deallocated object (zombie) at address: 0xf583270. 在仪表这里是我所看到的。仪器突出显示这条线,并在其旁边有100%。

AViewController *a = [[AViewController alloc]init]; 

回答

0

内存管理的东西,需要多一点的时间来太肯定的。我亲自让操作手柄为我处理所有事情。这意味着每次我分配一些东西,我只是给它一个autorelease。操作系统将在需要时为我处理该版本。唯一的问题是当你在同一个作用域中重用一个对象时,操作系统会发送太多的发布并释放内存,然后再使用它。下面是一个例子

//This code will result in a memory crash 
CustomObject *coolThing = [[[CustomObject alloc] init] autorelease]; 
[coolThing setAwesomeLevel:10]; 
[array addObject:coolThing]; 

[coolThing setAwesomeLevel:7]; 
[array2 addObject:coolThing]; 

相反,你会使用

//Working code 
CustomObject *coolThing = [[CustomObject alloc] init]; 
[coolThing setAwesomeLevel:10]; 
[array addObject:coolThing]; 

[coolThing setAwesomeLevel:7]; 
[array2 addObject:coolThing]; 

[coolThing release]; 

现在,使用自动释放你的代码,你会做的是将它们添加到allocs。这就是为什么你的代码泄漏。当您将其添加到webView对象时,它将增加其保留帐户。当你离开这个范围时,它有一个2的保留帐户,但你只发送一次发布(其保留将保持为1,并且从不释放内存)。

UISwipeGestureRecognizer *swipeRight = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)] autorelease]; 
[(UISwipeGestureRecognizer *)swipeRight setNumberOfTouchesRequired:2]; 

swipeRight.direction = UISwipeGestureRecognizerDirectionRight; 
swipeRight.delegate = self; 
[webView addGestureRecognizer:swipeRight]; 

UISwipeGestureRecognizer *swipeLeft = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)] autorelease]; 
[(UISwipeGestureRecognizer *)swipeLeft setNumberOfTouchesRequired:2]; 
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; 
swipeLeft.delegate = self; 
[webView addGestureRecognizer:swipeLeft]; 

如果您不想像这样使用autorelease,则需要在将手势添加到webView后简单添加一些版本。

[swipeRight release]; 
[swipeLeft release] 
+0

很好的解释!我仍然习惯于内存管理,这很有帮助! – Sum

+0

顺便说一句,我添加了一个额外的代码的问题。那会是我想要使用autorelease的情况吗?如果我们快速地来回切换,这不是一个问题吗? – Sum

+1

您添加的代码是正确的。添加一个autorelease并删除最终版本将导致相同的代码。这不会是我上面提到的问题,因为您不会在相同范围内更改对象。我上面描述的问题非常少见,每当我提到autorelease功能时,我都会提及它。 – ColdLogic

4

你是分配/初始化UISwipeGestureRecognizer(这使得你的工作,释放),而不是释放它在你的上面一段代码,两次。在将这些添加到您的webview后,您需要添加[swipeRight release];[swipeLeft release];

+0

明白了。我正在测试它! – Sum

+0

似乎已经解决了大部分手势识别器泄漏。 :) – Sum

+0

但是,另外一个问题,我添加了一些问题(代码不会在评论中格式化) – Sum

1

添加手势大家的意见后,调用就可以了release方法,因为手势是保留意见在其中添加。

像下面对象的C

[webView addGestureRecognizer:swipeRight]; 
    [swipeRight release]; 

而且

[webView addGestureRecognizer:swipeLeft]; 
    [swipeLeft release]; 
+0

对,我认为应该解决它。我不知道我怎么了。我正在测试,现在:) – Sum