init] 自动引用计数

发布于 2024-11-17 08:14:01 字数 344 浏览 2 评论 0原文

我知道我应该使用:

ObjectClass *tmpObject = [[ObjectClass alloc] init];
realObject = tmpObject;
[tmpObject release]

初始化realObject(其中realObject是类中的一个对象)

但是现在使用ARC模式,释放是自动的,我还需要吗使用这种技术? 我可以简单地使用realObject = [[ObjectClass alloc] init];吗? 如果没有的话,有什么具体原因会泄漏吗?

谢谢

I know that I am suppose to use:

ObjectClass *tmpObject = [[ObjectClass alloc] init];
realObject = tmpObject;
[tmpObject release]

to initialise realObject (where realObject is an object within a class)

But now with ARC mode, releasing is automatic, do i still need to use this technique?
Can I simply use realObject = [[ObjectClass alloc] init];?
If not is there any specific reason why it would leak?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ペ泪落弦音 2024-11-24 08:14:01

正如 Spencer 所说,如果您在启用 ARC 的情况下进行编译,则根本无法调用 release。这样做是错误的,编译器会为您解决这个问题。

然而:

ObjectClass *tmpObject = [[ObjectClass alloc] init];
realObject = tmpObject;
[tmpObject release]

在这种情况下,tmpObject 对于 ARC 和手动保留释放来说是完全没有意义的。事实上,在手动保留释放中,上面的代码将立即释放分配的对象,导致它被释放(除非 ObjectClass 内部做了一些奇怪的事情)和 realObject将留下一个悬空指针。

也就是说,当有人第一次尝试向 realObject 发送消息时,所编写的代码将导致崩溃。

澄清一下:

ObjectClass *tmpObject = [[ObjectClass alloc] init];
// tmpObject now contains a reference to an instance of ObjectClass; say, 0x12340
realObject = tmpObject;
// realObject now contains a reference to that same instance; realObject == 0x12340
[tmpObject release]
// this releases the object
// both tmpObject and realObject now reference a deallocated object; much hilarity ensues.    

对于 ARC,你只需这样做:

realObject = [[ObjectClass alloc] init];

As Spencer said, if you compile with ARC enabled, you cannot call release at all. It is an error to do so and the compiler takes care of it for you.

However:

ObjectClass *tmpObject = [[ObjectClass alloc] init];
realObject = tmpObject;
[tmpObject release]

The tmpObject in that case is entirely pointless for both ARC and manual retain-release. And, in fact, in manual retain-release, the above code will immediately release the object allocated, causing it to be deallocated (unless ObjectClass internally does something odd) and realObject will be left with a dangling pointer.

I.e. that code, as written, will cause a crash the first time anyone tries to message realObject.

To clarify:

ObjectClass *tmpObject = [[ObjectClass alloc] init];
// tmpObject now contains a reference to an instance of ObjectClass; say, 0x12340
realObject = tmpObject;
// realObject now contains a reference to that same instance; realObject == 0x12340
[tmpObject release]
// this releases the object
// both tmpObject and realObject now reference a deallocated object; much hilarity ensues.    

For ARC, you just do this:

realObject = [[ObjectClass alloc] init];
鹤舞 2024-11-24 08:14:01

如果您使用 -fobjc-arc 进行编译(即使用 ARC),那么您不仅不需要调用 release,如果这样做还会出现编译器错误。使用 ARC 时,编译器的工作就是为您插入 retainrelease 调用。

If you are compiling with -fobjc-arc (ie, using ARC) then not only do you not need to call release, it is a compiler error if you do so. When using ARC, it is the job of the compiler to insert retain and release calls for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文