在使用ARC时,正确地在视图之间传递NSManagedObjects ?内存警告&崩溃

时间:2022-10-06 14:20:20

Hi I have a nasty memory management issue under ARC and cannot figure out how to solve it. The problem stands like this, I have these objects:

你好,我有一个严重的内存管理问题在弧线下,无法解决它。问题是这样的,我有这些对象

ObjectManager - a singleton that does fetchRequests to core data and fetches one or more NSManagedObjects

ObjectManager——一个单例对象,它向核心数据发出请求并获取一个或多个nsmanagedobject

UIViewControllerA - a view controller in which I have a button "PassManagedObject" and a property declared as below:

UIViewControllerA——一个视图控制器,其中我有一个按钮“PassManagedObject”,属性声明如下:

@property (strong, nonatomic) ManagedObject *objectForToday;

in viewDidLoad on UIViewControllerA, I call the method refreshDailyObject which does this:

在uiviewviewcontrollera上的viewDidLoad中,我调用refreshDailyObject方法,它可以做到:

self.objectForToday = nil;
self.objectForToday  = [[ObjectManager sharedManager] getDailyObject];

if I tap the PassManagedObject button I create UIViewControllerB, pass the objectForToday to it and display it, see below

如果我点击创建UIViewControllerB的PassManagedObject按钮,将objectForToday传递给它并显示它,请参见下面

- (IBAction)passManagedObjectTapped:(id)sender {
       UIViewControllerB *viewController = [[UIViewControllerB alloc] initWithNibName:@"UIViewControllerB"];
       viewController.object = self.objectForToday;
       [self.navigationController pushViewController:viewController animated:NO];
 }

UIViewControllerB has a property declared like this:

UIViewControllerB有这样声明的属性:

@property (strong, nonatomic) ManagedObject *object;

and a button "Back" which does this:

还有一个按钮"Back"

- (IBAction)backAction:(id)sender {
    self.object = nil;
    [self.navigationController popViewControllerAnimated:NO];
}

Now the problem is this. If I tap continuously passManagedObjectTapped and then backAction, and again passManagedObjectTapped and again backAction and then again passManagedObjectTapped and automate this I'm getting eventually Received Memory Warning 1, and then crash.

现在的问题是。如果我不断地点击passManagedObjectTapped然后反向操作,再一次passManagedObjectTapped,再一次反向操作然后再一次passmanagedobjecttapping并自动执行我最终收到了内存警告1,然后崩溃了。

The Instruments doesn't show any leaks but my memory allocation keeps slowly going up.

仪器没有显示任何泄漏,但我的内存分配继续缓慢上升。

I am using ARC under iOS4.3 & iOS5. I've been struggling to figuring out what is wrong for a day now. Any help will be highly appreciated.

我在iOS4.3和iOS5下使用ARC。今天我一直在努力弄清楚哪里出了问题。非常感谢您的帮助。

Thanks!

谢谢!

1 个解决方案

#1


5  

The self.object = nil; and self.objectForToday = nil; isn't necessary - ARC and the synthesized properties setters already take care of that.

自我。对象=零;和自我。objectForToday =零;不需要- ARC和合成属性设置器已经处理好了。

It seems pretty likely that you have a circular reference somewhere. Do you have any situations where object A has a strong reference to object B and object B has a strong reference to object A?

似乎你在某处有一个循环引用。你是否遇到过物体A对物体B有强烈的引用而物体B对物体A有强烈的引用?

If so, just change one of those references to weak instead (or assign if you want to support iOS 4.3).

如果是这样,只需将其中一个引用改为弱引用(或者如果您想支持ios4.3)。

#1


5  

The self.object = nil; and self.objectForToday = nil; isn't necessary - ARC and the synthesized properties setters already take care of that.

自我。对象=零;和自我。objectForToday =零;不需要- ARC和合成属性设置器已经处理好了。

It seems pretty likely that you have a circular reference somewhere. Do you have any situations where object A has a strong reference to object B and object B has a strong reference to object A?

似乎你在某处有一个循环引用。你是否遇到过物体A对物体B有强烈的引用而物体B对物体A有强烈的引用?

If so, just change one of those references to weak instead (or assign if you want to support iOS 4.3).

如果是这样,只需将其中一个引用改为弱引用(或者如果您想支持ios4.3)。