在 Objective-c 中,如果不再有指针指向 autoReleased 对象,它们会被释放吗?
只是出于好奇...如果我更改指针以指向其他自动释放实例,则指针指向的前一个自动释放实例是否会正常自动释放?例如...
NSString pStr* = [NSString stringWithString:@"instance One"];
// do sth with pStr
pStr = [NSString stringWithString:@"Instance two"];
// do sth else with pStr
两个实例都是自动释放的吗?
Just from Curiosity... If I alter a pointer to point to an other autoreleased instance, the previous autoreleased instance that the pointer was pointing to, is autoreleased normally ? For example ...
NSString pStr* = [NSString stringWithString:@"instance One"];
// do sth with pStr
pStr = [NSString stringWithString:@"Instance two"];
// do sth else with pStr
Are Both of the instances autoreleased ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。对象的生命周期并不真正取决于哪些变量指向它们(与垃圾收集的环境不同)。这就是为什么变量可能指向已释放的对象或内存泄漏:对象永远保留而没有对它们的引用。
生命周期取决于对象的创建方式以及如何使用
retain
、release
和autorelease
方法。Cocoa 内存管理指南中有更详细的解释。
Yes. Lifecycle of objects doesn't really depend on which variables point to them (unlike environments with garbage-collection). That's why it's possible to have variable pointing to deallocated object or have memory leaks: objects retained forever with no references to them.
Lifecycle depends on how objects are created and how you use
retain
,release
andautorelease
methods.It's explained in more details in Cocoa memory management guide.