繁体   English   中英

Memory 管理与手势识别器

[英]Memory Management with Gesture Recognizer

我的很多 memory 泄漏都来自此识别滑动的代码。 我究竟做错了什么? 第一行是我认为正在泄漏的东西(使用仪器)。 它被显示为许多错误的负责调用者 这是在 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. 在仪器中,这就是我所看到的。 Instruments 突出显示了这条线,并且旁边有 100%。

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

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

将手势添加到您的视图后,调用它的release方法,因为手势由您添加的视图保留

像下面

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

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

object-c 中的 Memory 管理肯定也需要一些时间来使用。 我个人让运营为我处理一切。 这意味着每次我分配一些东西时,我只是给它一个自动释放。 操作系统会在需要时为我处理发布。 唯一出现问题的情况是,当您在同一个 scope 中重用 object 时,操作系统会发送太多版本并在您想要它之前释放 memory。 这是一个例子

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

现在,要在您的代码中使用自动释放,您所要做的就是将它们添加到分配中。 这就是您的代码泄漏的原因。 当您将其添加到 webView object 时,它正在增加其保留帐户。 当您离开 scope 时,它的保留帐户为 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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM