gpt4 book ai didi

objective-c - 弧 : When is ephemeral view controller delegate memory reclaimed with weak reference

转载 作者:行者123 更新时间:2023-12-04 04:54:26 25 4
gpt4 key购买 nike

这是来自 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

BarViewController.m
#import "BarViewController.h"

@interface BarViewController ()

@end

@implementation BarViewController

@end

FooViewController.h
#import <UIKit/UIKit.h>

@interface FooViewController : UIViewController

@end

FooViewController.m
#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报告通过其代表选择的内容。

在我上面的代码中,当我设置 barnilFooViewController.m ,为 bar.delegate 预留的内存会发生什么情况? ?一方面,我认为设置 barnil可能会导致 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.

Here's a decent Apple doc on the subject.

关于objective-c - 弧 : When is ephemeral view controller delegate memory reclaimed with weak reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16962482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com