您好,我在 ARC 下遇到了一个严重的内存管理问题,不知道如何解决。问题是这样的,我有这些对象:
ObjectManager - 一个对核心数据执行 fetchRequests 并获取一个或多个 NSManagedObjects 的单例
UIViewControllerA - 一个 View Controller ,其中我有一个按钮“PassManagedObject”和一个声明如下的属性:
@property (strong, nonatomic) ManagedObject *objectForToday;
在 UIViewControllerA 上的 viewDidLoad 中,我调用方法 refreshDailyObject 执行此操作:
self.objectForToday = nil;
self.objectForToday = [[ObjectManager sharedManager] getDailyObject];
如果我点击 PassManagedObject 按钮,我将创建 UIViewControllerB,传递 objectForToday 并显示它,见下文
- (IBAction)passManagedObjectTapped:(id)sender {
UIViewControllerB *viewController = [[UIViewControllerB alloc] initWithNibName:@"UIViewControllerB"];
viewController.object = self.objectForToday;
[self.navigationController pushViewController:viewController animated:NO];
}
UIViewControllerB 有一个声明如下的属性:
@property (strong, nonatomic) ManagedObject *object;
和一个按钮“返回”,它执行以下操作:
- (IBAction)backAction:(id)sender {
self.object = nil;
[self.navigationController popViewControllerAnimated:NO];
}
现在的问题是这样的。如果我连续点击 passManagedObjectTapped 和 backAction,然后再次 passManagedObjectTapped 和 backAction,然后再次 passManagedObjectTapped 并自动执行此操作,我最终会收到Received Memory Warning 1,然后崩溃。
Instruments 没有显示任何泄漏,但我的内存分配一直在缓慢增加。
我在 iOS4.3 和 iOS5 下使用 ARC。一天以来,我一直在努力找出问题所在。任何帮助将不胜感激。
谢谢!
最佳答案
self.object = nil;和 self.objectForToday = 零;不是必需的 - ARC 和合成属性 setter 已经解决了这个问题。
很可能您在某处有循环引用。你有没有遇到对象A强引用对象B,对象B强引用对象A的情况?
如果是这样,只需将其中一个引用更改为 weak (或者如果你想支持 iOS 4.3 则分配)。
关于objective-c - 使用 ARC 时在 View 之间正确传递 NSManagedObjects?内存警告和崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9238750/