强制对象在ARC下取消分配 [英] Forcing an object to deallocate under ARC

查看:106
本文介绍了强制对象在ARC下取消分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一款iPad照片拼贴应用程序,可以一次在屏幕上绘制数百个 UIImageView

I'm working on an iPad photo collage app that draws perhaps hundreds of UIImageViews on the screen at once.

有一个按钮,让用户重新创建,这是假设运行循环到 [photo removeFromSuperview]

There is a button that lets the user "re-create", which is suppose to run a for loop to [photo removeFromSuperview] on all photos and then initialize a new batch, in that order.

我使用ARC,我的控制台告诉我,我的照片' dealloc 方法在下一个批处理完成后才被调用,这意味着我正在运行

I'm using ARC, and my console tells me that my Photo's dealloc method isn't being called until AFTER the next batch has been drawn, meaning I'm running into memory issues, even though I'm trying to remove the first set before adding the next set.

有一种方法要么1)等到所有的照片都已经被删除正确解开或2)强制所有照片dealloc立即在ARC下?

Is there a way to either 1) wait until all the photos have been properly dealloc'd or 2) force all the photos to dealloc immediately under ARC?

推荐答案

一个自动释放池没有实现它。您可以通过在自己的for循环中包装自己的自动释放池来修复此问题。

You are probably putting your image views in an autorelease pool without realizing it. You may be able to fix this by wrapping your own autorelease pool around your for-loop.

例如,我做了一个非常简单的测试项目,按钮在我的顶层视图下。当我点击按钮,它删除图像视图,并创建一个新的。它通过循环顶级视图的子视图来删除图像视图。这里是代码:

For example, I made a very simple test project with one image view and one button under my top-level view. When I tap the button, it removes the image view and creates a new one. It removes the image view by looping over the top-level view's subviews. Here's the code:

@implementation ViewController

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

- (IBAction)redoWasTapped:(id)sender {
    [self destroyImageView];
    [self initImageView];
}

- (void)destroyImageView {
    for (UIView *subview in self.view.subviews) {
        if ([subview isKindOfClass:[UIImageView class]]) {
            [subview removeFromSuperview];
        }
    }
}

- (void)initImageView {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"picture.jpg"]];
    imageView.frame = CGRectInset(self.view.bounds, 100, 100);
    [self.view addSubview:imageView];
}

@end

启用记录引用计数的分配工具,我看到每个删除的映像视图在 destroyImageView 期间未被释放。相反,稍后当运行循环调用 - [NSAutoreleasePool release] 时,它被释放。

When I ran this under the Allocations instrument with "Record reference counts" enabled, I saw that each removed image view was not deallocated during destroyImageView. Instead, it was deallocated later when the run loop called -[NSAutoreleasePool release].

c $ c> destroyImageView 以管理自己的自动释放池:

Then I changed destroyImageView to manage its own autorelease pool:

- (void)destroyImageView {
    @autoreleasepool {
        for (UIView *subview in self.view.subviews) {
            if ([subview isKindOfClass:[UIImageView class]]) {
                [subview removeFromSuperview];
            }
        }
    }
}

我再次在仪器下运行它,我看到每个删除的图像视图在 destroyImageView 中,在 @autoreleasepool block。

When I ran it again under Instruments, I saw that each removed image view was deallocated during destroyImageView, at the end of the @autoreleasepool block.

这篇关于强制对象在ARC下取消分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆