NSKeyedUnarchiver 会自动释放吗?

发布于 2024-08-28 19:26:07 字数 131 浏览 13 评论 0原文

我正在对属性列表进行一些归档,当我使用 NSKeyedUnarchiver 取消归档我的数据时,我发现如果我随后释放该对象,我的应用程序会崩溃。我想知道 finishDecoding 消息是否也会自动释放该对象。看起来很奇怪,当我释放它时它崩溃了。

I'm doing some archiving to a property list and when I unarchive my data using NSKeyedUnarchiver I find that my app crashes if I release the object afterward. I was wondering if the finishDecoding message also autoreleases the object. Seems weird that it crashes when I release it.

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

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

发布评论

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

评论(2

人事已非 2024-09-04 19:26:07

你释放什么? NSKeyedUnarchiver 或未归档的对象?

你是否应该释放 NSKeyedUnarchiver 取决于你如何创建它。它遵循通常的规则。如果您使用 alloc + initForReadingWithData 您应该释放,如果您使用 unarchiveObjectWithDataunarchiveObjectWithFile - 您不应该。

关于decodeObjectForKey的结果,它也遵循通常的规则,即方法返回自动释放的对象,除非明确保留它,否则不应释放它。

关于finishDecoding:您应该在释放 NSKeyedUnarchiver 对象之前显式调用它。此后您不应该使用 unarchiver,但对象仍然存在。

What do you release? NSKeyedUnarchiver or unarchived object?

Should you release NSKeyedUnarchiver or not depends on how you created it. It follows usual rules. If you use alloc + initForReadingWithData you should release, if you use unarchiveObjectWithData or unarchiveObjectWithFile - you shouldn't.

Regarding result of decodeObjectForKey, it also follows usual rule that method returns autoreleased object and you shouldn't release it unless you explicitly retained it.

Regarding finishDecoding: you are expected to explicitly call it before NSKeyedUnarchiver object is released. You shouldn't use unarchiver after that point, but object is still alive.

深巷少女 2024-09-04 19:26:07

如果您使用 初始化解档器

[[NSKeyedUnarchiver alloc] initForReadingWithData:data]

那么您有责任在完成解档后释放它。

如果它在您发布后崩溃,那么很可能您的错误在其他地方。可能是因为您正在使用解档器提供的对象而不保留它们。当解档器被解除分配时,它会释放为您提供的对象。如果您没有保留这些对象,它们将被释放,并且当您引用这些对象时将会崩溃。

If you initialize the unarchiver with

[[NSKeyedUnarchiver alloc] initForReadingWithData:data]

Then you are responsible for releasing it when yo are done unarchiving.

If it's crashing after you release it, then it's likely your bug is elsewhere. Probably because you are using objects that your unarchiver provides without retaining them. And when the unarchiver is deallocated, it releases the objects that provided for you. If you didn't retain these, they will be deallocated and you will crash when you reference these objects.

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