简体   繁体   English

ARC:对象-委托关系

[英]ARC: Object - Delegate relationship

I am developing an ARC enabled project. 我正在开发一个支持ARC的项目。 From a view controller I am pushing MyClass , 从视图控制器,我推MyClass

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

After doing some operations I am popping MyClass. 完成一些操作后,我弹出MyClass。 The problem here is that MyClass is not getting deallocated . 这里的问题是MyClass没有被释放 Following is how the classes look. 以下是这些类的外观。

/* MyHelperClassDelegate */ / * MyHelperClassDelegate * /

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

/* MyHelperClass Interface */ / * MyHelperClass接口* /

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

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

- (void)startHelping;

@end

/* MyHelperClass Implementation */ / * MyHelperClass实现* /

@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. MyHelperClass使用NSMutalbleURLRequestNSURLConnection调用Web服务以获取一些数据并将其保存为用户默认值。

One thing to notice here is, if I comment the line helper.delegate = self; 这里要注意的一件事是,如果我评论helper.delegate = self; , then MyClass gets deallocated. ,然后将MyClass释放。

What to do to make MyClass get deallocated when it is popped out of navigation controller? 当MyClass从导航控制器中弹出时,该如何做才能使其释放?

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). 您的委托代码看起来正确(除了使用ivar,不显示@synthesize,因此您可能同时具有_delegate和委托)。 Its quite likely that something else is retaining MyClass. 很可能还有其他东西保留了MyClass。 What I suggest you do is add a NSLog to your MyClass dealloc. 我建议您做的是在您的MyClass dealloc中添加一个NSLog。 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. 如果不是,那么请仔细查看您在viewDidLoad等中所做的工作,并开始注释掉该代码的各个部分,直到获得解除分配为止。

Also, I assume you don't keep a strong reference in the class that pushes the MyClass object. 另外,我假设您在推送MyClass对象的类中未保留强引用。

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. 但是未取消分配MyClass对象的一个​​原因可能是由于delegate被声明为strong ,而它被您的helper对象保留了,并且MyClass对象的helper属性也被宣布为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 . 诀窍可能在于您使用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 : 没有指定如何使用已提供的代码使用此类,但是请注意NSURLConnection类参考中引用的特殊注意事项:

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. 当连接完成加载,失败或取消时,它将释放该强引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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