问题描述
我在IOS Simulator中遇到了segfault 11内存访问错误,但是当我在下面的代码中注释掉该版本时,该错误消失了.
I was getting a segfault 11 memory access error in the IOS Simulator, but it disappears when I comment out the release in the code below.
// get get the question number
NSString *text = [attributeDict valueForKey:XML_TAG_QUESTION_ATTRIBUTE_NUMBER];
question.number = [text intValue];
//[text release]; <==== no more segfault 11 when this is commented out.
我的问题是,由于我收到了NSXMLParser
实现返回的NS String实例,因此引用计数是否增加了,我是否应该释放它?
My question is, since I am receiving an instance of NS String returned by the NSXMLParser
implementation, isn't the reference count increased and should I not be releasing it?
推荐答案
这是规则:在内存管理中始终使用NARC.
Here's the rule: Always NARC on your memory management.
如果您致电:
(N)ew
(A)lloc
(R)tain或
(C)opy ...
If you call:
(N)ew
(A)lloc
(R)etain or
(C)opy...
您需要释放.如果没有,您可以通过一种方便的方法获得它,并且它会自动发布.
You need to release. If not, you got it through a convenience method and it's autoreleased.
对于包含其他对象的容器,该容器保留了对象,在释放该容器之前,您不必担心.
In the case of containers of other objects, the container has the objects retained, and you don't need to worry about it until you release the container.
这篇关于是否应释放此值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!