Pregunta

I am debugging an interesting issue with my app.

The app is targeted at iOS6.1 and it is using ARC. Here is some background since it is too large to copy paste into SO.

There is a Dashboard class that has a child view controller. This child view controller is rather large, it has buttons two scroll views that get dynamically created based on the CoreData object that The dashboard controller hands to it. WHen the dash board controller is done with the child controller it destroys it and creates another with a different core data object.

One of the views in the child controller that gets dynamically created and stuffed into a UIScrollView is a subclass of UIWebView. Lets call it SubWeb. When an instance of SubWeb is created it uses an NSOperationBlock to do some file fetching over the network along with some other stuff (write to disk, encryption etc). Once it is done, it needs to be able to insert a file or webpage into the SubWeb instance.

I solved for this by giving SubWeb a strong property. This is my problem: My clean up is never able to destroy the child view controller or the SubViews; I have confirmed this with an instruments allocation study that targets my classes. by by creating and destroying child view controllers (and SubWeb objects) I can watch the memory allocations for both the child view controller and the Sub wWeb objects staircase up until the app crashes. What is weird is that when I set the NSBlockOperation to weak, the destroy routine does what I expect.

I need the strong reference because I only front load a few SubWeb objects with a real file to start, then if user scrolls over to them I lazy load the needed file by pushing the property that

Here is WebView I am referring to when the child view controller and instances of SubWeb view are not cleaned up:

#import <UIKit/UIKit.h>

@interface MySubWebView : UIWebView

@property (strong) NSBlockOperation *fileLoadOperation;

@end

Here is the WebView when it is cleaned up (But - now the lazy Blocak operations are null whenn I need them:

#import <UIKit/UIKit.h>

@interface MySubWebView : UIWebView

@property (weak) NSBlockOperation *fileLoadOperation;

@end

The only difference is the strong and the weak type. Has anyone ever used NSBLockOperation for this type of lazy loading action? Is there a defferent/better way I might use to solve the lazy load?

¿Fue útil?

Solución

I'm not following your logic for making the NSBlockOperation object strong. It should generally be weak. If the operation completes, there's no reason to keep the operation object any more.

In answer to the question on your memory consumption, you're running out of memory because you undoubtedly have a strong reference cycle (a.k.a. retain cycle). If the block has a reference to self (whether explicitly, or implicitly by referencing some of your controller's instance variables), you can end up with a strong reference cycle (i.e. two objects that maintain strong references to each other, and thus neither will ever get deallocated unless you manually clean it up.)

For information about retain cycles see Use Weak References to Avoid Retain Cycles in the Advanced Memory Management Programming Guide. Also see Avoid Strong Reference Cycles when Capturing self in the Programming with Objective-C guide.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top