作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是来自 previous one 的后续问题这可能包含了太多的间接细节。考虑以下代码:
BarViewController.h
#import <UIKit/UIKit.h>
@protocol SomeDelegate
- (void)someCallback; // doesn't matter
@end
@interface BarViewController : UIViewController
@property (weak, nonatomic) id <SomeDelegate> delegate;
@end
#import "BarViewController.h"
@interface BarViewController ()
@end
@implementation BarViewController
@end
#import <UIKit/UIKit.h>
@interface FooViewController : UIViewController
@end
#import "FooViewController.h"
#import "BarViewController.h"
@interface FooViewController () <SomeDelegate>
@end
@implementation FooViewController
- (void)viewDidLoad
{
[super viewDidLoad];
BarViewController *bar = [[BarViewController alloc] init];
// does this assignment create a "strong" reference i.e. increase retain count by 1?
bar.delegate = self;
// *do some useful stuff with bar.delegate here* //
bar = nil; // is memory for bar.delegate free'd here,
// or only after this instance of FooViewController is destroyed?
}
#pragma mark - SomeDelegate
- (void)someCallback {
// doesn't matter
}
@end
FooViewController
是一些小型应用程序中的主视图 Controller ,而
BarViewController
只是一些短暂的事情,可能旨在让用户选择几个按钮之一。
BarViewController
报告通过其代表选择的内容。
bar
至
nil
在
FooViewController.m
,为
bar.delegate
预留的内存会发生什么情况? ?一方面,我认为设置
bar
至
nil
可能会导致
bar.delegate
也设置为
nil
作为
bar
的一部分对象被释放。另一方面,我对
weak
的理解引用是只有在没有人强烈指向它时才会释放内存。所以如果
bar.delegate = self
创建对
delegate
的强引用(是吗?),有可能
bar.delegate
不知何故,仍然拥有指向我们的
FooViewController
实例的指针, 即使
bar
现在是
nil
?这里有内存泄漏的可能性吗?
UIWindow
会强烈指向我们的
FooViewController
实例作为 Root View Controller ,我们的 BarViewController 实例将弱指向我们的
FooViewController
实例作为其代表。所以即使我们设置
bar = nil
,
bar.delegate
弱指向一个对象,该对象至少有一个来自
UIWindow
的强指针, 所以
bar.delegate
不能基于“当没有其他人强烈指向它时释放一个弱属性”的前提来释放它?
最佳答案
除了这里,你几乎做对了:
On the other hand, my understanding of a weak reference is that the memory is only released once there is no longer anybody pointing strongly to it. So if bar.delegate = self creates a strong reference to delegate (does it?)
weak
比你想象的要简单得多。就像
assign
,除了弧生成的 dealloc,它的类将它设置为 nil。并且,
bar.delegate = self
;不会“创建对委托(delegate)的强引用”,它只是分配一个指向 Foo 实例的指针,当 Bar 被释放时该实例将被清除。
// no bar exists
BarViewController *bar = [[BarViewController alloc] init];
// now a bar exists with only a stack variable referring to it, it will be released
// by arc at the bottom of this method
// YOUR QUESTION: does this assignment create a "strong" reference i.e. increase retain count by 1
bar.delegate = self;
// Answer - No. No change in retain counts. Bar now has an assigned pointer to the
// Foo instance (self)
// *do some useful stuff with bar.delegate here* //
bar = nil; // is memory for bar.delegate free'd here,
// bar is free'd here, and would have been whether or not you set it to nil
// moreover - bar's delegate is set to nil because it was declared weak.
关于objective-c - 弧 : When is ephemeral view controller delegate memory reclaimed with weak reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16962482/
我是一名优秀的程序员,十分优秀!