将对象设置为 nil 时不会调用 dealloc 方法

2023-12-20

我有个问题。

我首先创建了一个扩展 NSObject 的对象,我提供了描述和 dealloc 方法的重写。这是我的 Employee.m 文件:

@implementation Employee
.....

-(NSString *)description
{
    return [NSString stringWithFormat:@"Employ ID: %d has $%d value of assets", [self     employeeID], [self valueOfAssets]];
}

-(void)dealloc
{   
    NSLog(@"deallocating.. %@", self);
    [super dealloc];
}

在我的 main.m 中,我首先创建了一个 NSMutableArray 来保存 Employee 对象的列表:

NSMutableArray *employees = [[NSMutableArray alloc] init];

for (int i =0; i< 10; i++)
{
    // Create an instance of Employee
    Employee *person = [[Employee alloc] init];

    // Give the instance varaible interesting values
    [person setEmployeeID:i];
    [employees addObject: person];
}

最后我将员工设置为零

employees = nil;

我预计dealloc要调用每个 Employee 对象的方法,我会看到一些日志,例如:

deallocating.. Employ ID 0 has value.....
deallocating.. Employ ID 2 has value.....
....

但是,我没有看到任何日志,如果我在dealloc方法中,断点永远不会被命中。

有什么想法吗?


一些观察结果:

  1. person = nil不会释放非 ARC 代码中的对象。它将在 ARC 代码中(至少如果它很强大的话)。

  2. 在 ARC 中,本地对象超出范围时会自动为您释放。在非 ARC 中,超出范围的对象不会为您释放(如果您在其他地方没有对这些对象的其他引用,则最终会导致泄漏)。

  3. 将项目添加到可变数组中会增加该项目的保留计数,因此即使您在非 ARC 代码中包含释放,该对象也不会被释放,直到保留计数降至零(通过不仅释放来实现)将 person 对象添加到数组后,还将它们从数组中删除。

因此,鉴于这是非 ARC 代码,它可能类似于:

- (void)testInNonArcCode
{
    NSMutableArray *employees = [[NSMutableArray alloc] init]; // employees retain count = +1

    for (int i =0; i< 10; i++)
    {
        //create an instance of Employee
        Employee *person = [[Employee alloc] init]; // person retain count = +1

        //Give the instance varaible interesting values
        [person setEmployeeID:i];
        [employees addObject: person];  // person retain count = +2
        [person release];  // person retain count = +1 (YOU REALLY WANT TO DO THIS OR ELSE OR NON-ARC PROGRAM WILL LEAK)
        // person = nil;   // this does nothing, except clears the local var that's limited to the for loop scope ... it does nothing to reduce the retain count or improve memory management in non-ARC code, thus I have commented it out
    }

    // do whatever you want

    [employees removeAllObjects]; // this will remove all of the person objects and they will have their respective retain counts reduced to 0, and therefore the Employee objects will be released

    [employees release]; // employees array's own retain count reduced to zero (and will now be dealloced, itself)
}

在 ARC 代码中:

- (void)testInArcCode
{
    NSMutableArray *employees = [[NSMutableArray alloc] init]; // employees retain count = +1

    for (int i =0; i< 10; i++)
    {
        //create an instance of Employee
        Employee *person = [[Employee alloc] init]; // person retain count = +1

        //Give the instance varaible interesting values
        [person setEmployeeID:i];
        [employees addObject: person];  // person retain count = +2

        // person = nil;      // this would reduce person retain count to +1 (but unnecessary in ARC because when person falls out of scope, it will have it's retain count automatically reduced)
    }

    // do whatever you want

    [employees removeAllObjects]; // this will remove all of the person objects and they will have their respective retain counts reduced to 0, and therefore will be released

    // [employees release]; // not permitted in ARC
    // employees = nil;     // this would effectively release employees, but again, not needed, because when it falls out of scope, it will be released anyway
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将对象设置为 nil 时不会调用 dealloc 方法 的相关文章

  • iOS后台获取时间限制崩溃

    我已经设置了背景获取 使用大纲NSScreencast 第 92 集 http nsscreencast com episodes 92 background fetch BOOL application UIApplication app
  • Flutter-iOS 当应用程序更新/重新编译时存储的图像丢失

    嗨 请原谅我的英语 也是 flutter iOS 的新手 我在 iOS 上有一个关于 flutter 的应用程序 用户可以从他们的相机和图库中拍摄照片和视频 我正在使用 image picker 包 这没有问题 然后我将其保存在在其应用程序
  • Swift try inside Objective-C 块

    我需要创建一个函数foo它接受一个抛出闭包作为参数 我可以使用 Swift 或 ObjC 来实现它 但我需要能够从两者中调用它 像这样 Swift func bar throws func foo block throws gt void
  • 从 ios 应用程序到 symfony2 Web 应用程序的登录和管理会话

    我使用 Symfony2 开发了一个 Web 应用程序 我用过FOS用户包用于用户管理和安全 现在 我正在考虑为我的网络开发一个本机 iOS 应用程序 但并不真正知道如何解决从应用程序创建会话的问题 并在整个交互用户应用程序中维护它 换句话
  • 在 iOS 7 中查看 Core Data 创建的 sqlite 文件时出现问题

    当我尝试访问由核心数据创建的数据库文件时遇到问题 DB 文件位于 Documents 文件夹中 我已将其复制到桌面并使用 Firefox 插件打开它 Hello sqlite文件不包含任何行我有使用核心数据插入值 我正在使用 iOS 7 模
  • 如何本地化 iOS 故事板

    我有一个包含一些视图的 iPhone 故事板 例如 导航项标题名为News 应翻译为其他语言 当我向故事板添加新的本地化版本时 它会为新语言创建当前故事板的副本 在这里我可以更改导航项的标题 但对我来说它似乎不太有用 如果我的故事板包含 1
  • iOS 何时清理本地应用程序 ./tmp 目录?

    iOS什么时候清理本地应用程序 tmp目录 请注意 这不是一个骗局这个问题 https stackoverflow com questions 3593900 iphone storage in tmp directory 我问的是应用程序
  • 为什么 iPhone 应用程序的 main() 函数没有机会完成?

    考虑以下main 大多数 iPhone 应用程序都采用以下方法 int main int argc char argv NSAutoreleasePool pool NSAutoreleasePool alloc init int retV
  • 与 parse-server 和 auth0 的自定义身份验证集成

    我想将 auth0 com 与开源解析服务器结合使用 我当前的方法是通过 iOS 的 Lock 库使用标准登录从 auth0 获取令牌 使用该令牌 我想在解析服务器上调用自定义身份验证方法 该方法检查令牌是否有效 如果有效则将登录用户 我的
  • 在 Xcode 中查找未使用的文件

    我最近开始开发一个新应用程序 它基本上是我以前制作的应用程序的副本 但做了一些更改 为了制作这个新应用程序 我复制了旧应用程序并删除了一些不需要的内容 我想知道 有没有办法知道 Xcode 中正在使用哪些类文件 或者有什么关于如何查找未使用
  • Apple 由于崩溃而拒绝了我的应用程序,无法重现它

    我刚刚上传了一个应用程序到应用程序商店 它是为ios 7开发的 他们拒绝了该应用程序 因为我无法重现崩溃 他们向我发送了这份崩溃报告 Exception Type EXC BAD ACCESS SIGSEGV Exception Subty
  • 依赖于不同队列上的另一个操作的 NSOperation 无法启动

    我有操作的依赖图 并且使用多个队列来组织各种操作流 例如 peopleQueue sitesQueue sessionQueue sessionQueue loginOp fetchUpdatedAccountOp peopleQueue
  • iOS绘图3D图形库[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我正在搜索一个可以帮助我绘制 3D 图表的库 我想要类似的东西这一页 http www math uri edu bkaskosz fla
  • 增加 iPhone 的推送通知徽章

    是否可以在收到通知时增加徽章值 或者我应该将计数作为有效负载发送吗 如果我每次都将徽章值发送为 1 那么如果应用程序未打开 我如何增加应用程序图标中的徽章值 我已经使用了这段代码 但不起作用 void application UIAppli
  • 自动生成的 Swift 桥接标头中“找不到接口声明”

    我当前的项目包含 Swift 和 Objective C 代码 两种类型的源文件都使用另一种语言的代码 当我进行完全清理并重新编译时 几乎每个 Swift 类声明都出现错误Module Swift h 形式为 Cannot find int
  • 将 NSFetchedResultsController 添加到项目后出现问题

    我设置 CoreData 时没有NSFetchedResultsController一切都保存得很好 切换到之后NSFetchedResultsController 我在尝试保存图像时遇到奇怪的错误 这是我用来保存图像的代码 void sa
  • 找不到 Cocoa/Cocoa.h 文件

    我在用XMPPFramework在我的应用程序中 我已将 Cocoa Cocoa h 导入到我的 m 文件中 但是当我构建项目时Xcode显示错误 错误 未找到 Cocoa Cocoa h 文件 我该如何解决这个错误 如果您正在为 iOS
  • 如何从第二个视图弹回到根视图?

    我使用 2 将 3 个视图 根视图 第 1 个视图 第 2 个视图 连接在一起modal在 Apple Watch 故事板中继续 1 在根视图中 按下 保存 按钮后 将显示第一个模态视图 2 在第一模态视图中 一旦按下 500 按钮 将显示
  • 如何制作像 Facebook 应用程序一样的登录屏幕?

    如何制作像 Facebook 应用程序一样带有 电子邮件 和 密码 文本字段的登录屏幕 Facebook登入 http extdesenv com wp content uploads 2012 05 facebook login ios
  • TestFlight 安装的应用程序因 Swift 包管理器依赖项而崩溃

    我们已经迁移了一些 CocoaPod 依赖项 以便在 Xcode 11 中使用 Swift Package Manager 进行构建和链接 但是 每当我们将应用程序提交到 AppStore Connect 并使用 TestFlight 进行

随机推荐