使用手势识别器进行内存管理

时间:2011-07-15 20:05:57

标签: iphone ios xcode memory-management memory-leaks

我的很多内存泄漏来自于识别滑动的代码。我究竟做错了什么?第一行是我认为正在泄漏的东西(使用Instruments)。它被显示为许多错误的负责调用者 这是在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 =@"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];

3 个答案:

答案 0 :(得分:4)

你正在分配/初始化一个UISwipeGestureRecognizer(这使得你的工作就是释放它)而不是在你的顶层代码中释放它两次。在将这些内容添加到网络视图后,您需要添加[swipeRight release];[swipeLeft release];

答案 1 :(得分:1)

在您的观看中添加手势后,请在其上调用release方法,因为您添加的观看次数的手势保留

如下所示

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

并且

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

答案 2 :(得分:0)

object-c中的内存管理确实需要一些时间来使用。我个人让操作处理我的一切。这意味着每次我分配一些东西,我只是给它一个自动释放。操作系统将在需要时为我处理发布。这是一个问题的唯一一次是当你在同一范围内重用一个对象时,操作系统会发送太多版本并在你想要之前释放内存。这是一个例子

//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];

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

[swipeRight release];
[swipeLeft release]