在ObjC中,如何根据位置来描述alloc/copy/retain和auto-/release之间的平衡 [英] In ObjC, how to describe balance between alloc/copy/retain and auto-/release, in terms of location

查看:71
本文介绍了在ObjC中,如何根据位置来描述alloc/copy/retain和auto-/release之间的平衡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,在Objective-C中对alloc/copy/retain的调用意味着所有权,并且需要通过对autorelease/release的调用来平衡.您如何简洁地描述应该在哪里发生?单词简洁"是关键.我通常可以使用直觉来指导我,但我希望有一个明确的原则以防直觉失败,并且可以在讨论中使用.

As is common knowledge, calls to alloc/copy/retain in Objective-C imply ownership and need to be balanced by a call to autorelease/release. How do you succinctly describe where this should happen? The word "succinct" is key. I can usually use intuition to guide me, but would like an explicit principle in case intuition fails and that can be use in discussions.

属性简化了此事(规则是自动发布/释放发生在-dealloc和设置器中),但是有时属性不是一个可行的选择(例如,并非每个人都使用ObjC 2.0).

Properties simplify the matter (the rule is auto-/release happens in -dealloc and setters), but sometimes properties aren't a viable option (e.g. not everyone uses ObjC 2.0).

有时,发行版应位于同一块中.其他时候,分配/复制/保留以一种方法发生,该方法具有应在其中发生释放的相应方法(例如-init-dealloc).似乎是关键的是方法的这种配对(其中方法可以与自身配对),但是如何将其写成文字呢?此外,方法配对概念会丢失哪些情况?似乎没有涵盖释放属性的位置,因为设置器是自动配对的,并且-dealloc会释放不在-init中分配/复制/保留的对象.

Sometimes the release should be in the same block. Other times the alloc/copy/retain happens in one method, which has a corresponding method where the release should occur (e.g. -init and -dealloc). It's this pairing of methods (where a method may be paired with itself) that seems to be key, but how can that be put into words? Also, what cases does the method-pairing notion miss? It doesn't seem to cover where you release properties, as setters are self-paired and -dealloc releases objects that aren't alloc/copy/retained in -init.

感觉对象模型涉及到我的困难.我似乎没有模型可以附加保留/释放配对的元素.方法将对象从有效状态转换为有效状态,并将消息发送到其他对象.我看到的唯一自然配对是对象创建/销毁和方法输入/退出.

It feels like the object model is involved with my difficulty. There doesn't seem to be an element of the model that I can attach retain/release pairing to. Methods transform objects from valid state to valid state and send messages to other objects. The only natural pairings I see are object creation/destruction and method enter/exit.

背景: 这个问题的灵感来自:" NSMutableDictionary没有添加到NSMutableArray ".这个问题的提问者正在释放对象,但是可能会导致内存泄漏.分配/复制/保留调用通常由发行版来平衡,但是这种方式可能导致内存泄漏.全班都是代表.一些成员是在委托方法(-parser:didStartElement:...)中创建的,并在-dealloc中而不是在相应的(-parser:didEndElement:...)方法中释放.在这种情况下,属性似乎是一个很好的解决方案,但是仍然存在问题,即在不涉及属性的情况下如何处理释放.

Background: This question was inspired by: "NSMutableDictionary does not get added into NSMutableArray". The asker of that question was releasing objects, but in such a way that might cause memory leaks. The alloc/copy/retain calls were generally balanced by releases, but in such a way that could cause memory leaks. The class was a delegate; some members were created in a delegate method (-parser:didStartElement:...) and released in -dealloc rather than in the corresponding (-parser:didEndElement:...) method. In this instance, properties seemed a good solution, but the question still remained of how to handle releasing when properties weren't involved.

推荐答案

属性简化了此事(规则是在-dealloc和setter中自动执行/释放),但有时属性不是一个可行的选择(例如,并非每个人都使用ObjC 2.0).

Properties simplify the matter (the rule is auto-/release happens in -dealloc and setters), but sometimes properties aren't a viable option (e.g. not everyone uses ObjC 2.0).

这是对财产历史的误解.虽然属性是新的,但 accessors 一直是ObjC的关键部分.属性使编写访问器变得更加容易.如果您始终(并且应该)始终使用访问器,那么大多数这些问题都会消失.

This is a misunderstanding of the history of properties. While properties are new, accessors have always been a key part of ObjC. Properties just made it easier to write accessors. If you always use accessors, and you should, than most of these questions go away.

在拥有属性之前,我们使用Xcode的内置accessor-writer(在脚本">代码"菜单中),或使用有用的工具,例如

Before we had properties, we used Xcode's built-in accessor-writer (in the Script>Code menu), or with useful tools like Accessorizer to simplify the job (Accessorizer still simplifies property code). Or we just typed a lot of getters and setters by hand.

这篇关于在ObjC中,如何根据位置来描述alloc/copy/retain和auto-/release之间的平衡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆