简体   繁体   中英

ARC: Object - Delegate relationship

I am developing an ARC enabled project. From a view controller I am pushing MyClass ,

- (void)pushMyClass {
     MyClass *myClass = [[MyClass alloc] init];
     [self.navigationController pushViewController:myClass animated:YES];
}

After doing some operations I am popping MyClass. The problem here is that MyClass is not getting deallocated . Following is how the classes look.

/* MyHelperClassDelegate */

@protocol MyHelperClassDelegate <NSObject>
- (void)helperDidFinishHelping:(MyHelperClass *)helper;
@end

/* MyHelperClass Interface */

@interface MyHelperClass : NSObject {
    __weak id <MyHelperDelegate> delegate;
}

@property(nonatomic, weak) id<MyHelperDelegate> delegate;

- (void)startHelping;

@end

/* MyHelperClass Implementation */

@implementation MyHelperClass

@synthesize delegate;

- (void)dealloc {
    delegate = nil;
}

/* MyClass */

@interface MyClass : UIViewController <MyHelperClassDelegate> {
    MyHelperClass *helper;
}

@implementation MyClass {

    - (void)dealloc {
        helper.delegate = nil;
    }

    - (void)getHelp {
        helper = [MyHelperClass new];
        helper.delegate = self;
        [helper startHelping];
    }

    - (void)helperDidFinishHelping:(MyHelperClass *)helper {
    }
}

MyHelperClass calls a web service using NSMutalbleURLRequest & NSURLConnection to fetch some data and saves it to user defaults.

One thing to notice here is, if I comment the line helper.delegate = self; , then MyClass gets deallocated.

What to do to make MyClass get deallocated when it is popped out of navigation controller?

Thanks.

Your delegate code looks correct (except your use of an ivar, you don't show a @synthesize so you may have _delegate and delegate both). Its quite likely that something else is retaining MyClass. What I suggest you do is add a NSLog to your MyClass dealloc. Then push it, and immediately hit the back button and see if its dealloc'd or not. If not, then take a hard look at what you do in viewDidLoad et al and start commenting out sections of that code until you can get the dealloc.

Also, I assume you don't keep a strong reference in the class that pushes the MyClass object.

I agree with Chuck that one cannot say much from the code provided. But one reason why the MyClass object is not deallocated might be that it is retained by your helper object since delegate is declared as strong , and the MyClass object has the property helper also declared as strong. In this case you had a retain cycle, and none of them can be released.

The trick could possibly lie within the fact that you use NSURLConnection . It is not specified how you use this class with the code that you've provided, but please note the special considerations referenced in the NSURLConnection class reference :

Special Considerations: During the download the connection maintains a strong reference to the delegate. It releases that strong reference when the connection finishes loading, fails, or is canceled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM