- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在开发一个 iPad 照片拼贴应用程序,它可以同时在屏幕上绘制数百个 UIImageView
。
有一个按钮可以让用户“重新创建”,假设对所有照片运行 for
循环到 [photo removeFromSuperview]
然后初始化一个新的批处理,按照这个顺序。
我正在使用 ARC,我的控制台告诉我我的 Photo
的 dealloc
方法在绘制下一批之后才会被调用,这意味着我遇到了内存问题,尽管我试图在添加下一组之前删除第一组。
有没有办法 1) 等到所有照片都已正确解除分配或 2) 强制所有照片在 ARC 下立即解除分配?
最佳答案
您可能在没有意识到的情况下将 ImageView 放入自动释放池中。您可以通过将您自己的自动释放池包装在您的 for 循环周围来解决这个问题。
例如,我做了一个非常简单的测试项目,在我的顶级 View 下有一个 ImageView 和一个按钮。当我点击按钮时,它会删除 ImageView 并创建一个新 View 。它通过遍历顶级 View 的 subview 来删除 ImageView 。这是代码:
@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
当我在启用“Record reference counts”的 Allocations instrument 下运行它时,我看到每个移除的 ImageView 在 destroyImageView
期间都没有被释放。相反,它稍后在运行循环调用 -[NSAutoreleasePool release]
时被释放。
然后我更改了 destroyImageView
来管理它自己的自动释放池:
- (void)destroyImageView {
@autoreleasepool {
for (UIView *subview in self.view.subviews) {
if ([subview isKindOfClass:[UIImageView class]]) {
[subview removeFromSuperview];
}
}
}
}
当我在 Instruments 下再次运行它时,我看到每个移除的 ImageView 都在 destroyImageView
期间被释放,在 @autoreleasepool
block 的末尾。
关于objective-c - 强制对象在 ARC 下解除分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12827319/
我知道您可以使用 -fno-objc-arc 标志对 ARC 项目中不支持 ARC 的文件禁用 ARC。 有什么方法可以在非 ARC 项目中为支持 ARC 的文件启用 ARC 吗? 谢谢! 最佳答案
ARC 代表自动引用计数。 我刚刚升级了我的项目以使用 ARC。虽然不是全部。只是一些。 我怎么知道? 最佳答案 要查看默认值,您必须检查目标属性(您的目标 -> build设置 -> Apple L
我有一个框架 A,它是 ARC。这会消耗一些来自非 ARC 框架 B 的 API。 框架B示例代码(非ARC): @interface Settings : NSObject { NSDict
我一直避免升级 Xcode,直到某个特定项目完成(即将推出)。我希望能够在未来的项目中使用 ARC,同时保留维护和调查先前软件产品问题的能力。 问题是可以通过安装一次 Xcode 来完成此操作吗?当我
我正在开发一个非常旧的非基于 ARC 的项目,并添加了一些基于 ARC 的新 UIViewController(可以通过在构建阶段设置 -fobjc-arc 标志来实现) . 由于ARC和非ARC的混
这个问题在这里已经有了答案: How do you enable ARC project-wide in Xcode 4.2 (3 个答案) How to automatically conver
我是 iPhone 开发的新手。我了解到ARC是现在IOS引入的新特性。我的问题是,我有一个手动编写版本的旧非 ARC 项目,是否可以将该项目从非 ARC 切换到 ARC ? 谢谢, 拉吉 最佳答案
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 7年前关闭。 Improve this questi
我的项目是在 ARC 打开的情况下创建的,现在我使用的是第三方框架,该框架很可能不是用 ARC 构建的。这样做会有什么问题吗?我现在遇到了框架中某个地方发生的崩溃,但我没有得到有关崩溃的太多详细信息。
我猜这是一个简单的问题,但我无法弄清楚: 我的项目中存在三个与构建阶段中设置的链接器标志 -fno-obj-arc 不 ARC 兼容的文件。 但是据我所知,这些标志被忽略了。编译器仍然提示向对象发送保
我认为标题很好地解释了我的问题,我目前正在开发一个满足我个人需求的小型框架,并考虑使用 ARC 构建它(如果可能的话?),并在旧项目中使用它是在 ARC 之前 build 的吗? 最佳答案 是的,但有
我正在努力让现有的启用 ARC 的控件在不启用 ARC 的项目中在 Xcode 4.2 (OSX Snow Leopard) 下运行,并且我面临着各种问题: 我应该如何解决以下问题? 在属性中使用(s
我想知道如何更改 Arc 中的值,然后使 Arc 的其他副本再次有效。 use std::sync::Arc; use std::thread; use std::error::Error; use
ARC 运行时版本:我不知道,但我在这个网站上下载了它,https://chrome.google.com/webstore/detail/arc-welder/emfinbmielocnlhgmfk
当我将 iPhone 项目转换为 Objective - c ARC 时,我总是遇到相同的错误: /Applications/Xcode.app/Contents/Developer/Platform
如何在没有 ARC 的情况下为使用 ARC 的项目构建静态库? 我找到了一些静态库的源代码,我想为我的项目构建它,但我使用的是 ARC。 最佳答案 静态库可以在没有 ARC 的情况下构建,并且可以使用
我有一个使用 ARC 的 A 类和不使用 ARC 的其他 B 类和 C 类。如果 A 包含 B 类对象而 B 包含 C 类对象,那么 ARC 如何工作以及当 B 类和 C 类中的内存管理处理不当时会发
一旦我将 -fno-objc-arc 标志添加到构建阶段中的一个或多个单独的文件,我的 MyProject-Prefix.pch 文件就会进入非 ARC 模式,导致 150 条警告,如“警告:没有‘分
在某些情况下,我需要为几个非 ARC xcode 项目中的许多文件启用 ARC。我不想完全转换为 ARC,只需要使用 arc 启用几个文件。 有许多解决方案使用 XCode 控制台将 -fobjc-a
我还没有使用过 ARC,只是在它通过第 3 方代码强制进入项目时处理它。我已经阅读了所有 ARC 文档,但还没有看到这个问题的答案: 如果我在使用 -fobjc-arc 编译的模块中定义了一个类,我能
我是一名优秀的程序员,十分优秀!