简体   繁体   English

iPhone内存管理出现NSString问题

[英]iPhone memory management with NSString problem

@property (nonatomic,copy) NSString *orginalString;

..
NSString *tmpString =[[NSString alloc] init];
self.orginalString=tmpString;
[tmpString release];
NSString *newString =self.orginalString;  

What happens here to newString?, is this correct what I am doing? newString在这里会发生什么?,这是正确的吗?

orginalString retain count 1 at first, and when it is referenced with another pointer "newString" its retain count will be 2? orginalString首先保留计数为1,而当另一个指针“ newString”引用它时,其保留计数将为2? do I need to say "self.orginalString=nil" in the end? 我到底需要说“ self.orginalString = nil”吗? there are serious memory leaks but dont know it is something related with this. 有严重的内存泄漏,但不知道这与此有关。

  NSString *tmpString =[[NSString alloc] init]; 

tmpString is allocated and initialized tmpString已分配并初始化

  self.orginalString=tmpString; 

the string pointed to by tmpString is copied over to self.originalString (because the property is declared copy ); tmpString指向的字符串被复制到self.originalString (因为该属性被声明为copy );

  [tmpString release]; 

the string pointed to by tmpString is released, correctly; tmpString指向的字符串已正确释放; nothing happens to the string pointed to by self.orginalString ; self.orginalString指向的字符串没有任何self.orginalString

  NSString *newString =self.orginalString; 

a new pointer is created and initalized to point at the same string pointed to by self.orginalString ; 创建一个新的指针并使其指向由self.orginalString指向的同一字符串; nothing happens to self.orginalString retain count; self.orginalString保留计数没有任何self.orginalString it's just a second pointer pointing to the same object; 它只是指向同一对象的第二个指针;

at this point, if you don't release somewhere self.orginalString , it will be leaked. 在这一点上,如果您不释放self.orginalString ,它将被泄漏。

When you are dealing with memory management in OBjective C, my suggestion is not try and reason in terms of "retain count"; 当您在对象C中处理内存管理时,我的建议不是“保留计数”方面的尝试和推理。 retain count is just the mechanism that is used by the ObjC runtime to keep track of objects; 保留计数只是ObjC运行时用于跟踪对象的机制; it is too low-level and there are too many other objects around to increase or decrease the retain count, so that you immediately loose count. 它级别太低,周围还有太多其他对象,无法增加或减少保留计数,因此您立即失去了计数。

The best way, IMO, is reasoning in terms of ownership: when an object wants ownership of another, it will send a retain ; IMO最好的方法是根据所有权进行推理:当一个对象想要另一个所有权时,它将发送retain when it has done with it, it sends release . 完成后,将发送release Ownership is a local concept to a class, so it is easy to track down. 所有权是课程的本地概念,因此很容易追踪。

So, when you do: 因此,当您这样做时:

   NSString *newString =self.orginalString;  

newString is just a pointer to a not-owned object; newString只是一个指向非拥有对象的指针。 you do not need to balance that assignment with a release; 您无需在发布与发布之间取得平衡; on the contrary, if you do: 相反,如果您这样做:

   NSString *newString = [self.orginalString retain];

you are making yourself responsible for releasing the object when you have done with it. 使用完对象后,您就要自己负责释放对象。

You should visit this link . 您应该访问此链接 Actually, we should not check memory leaks with retain count, atleast for NSString. 实际上,我们不应该使用保留计数来检查内存泄漏,这至少是NSString。

To check memory leaks, always use Instruments coming with Xcode. 要检查内存泄漏,请始终使用Xcode随附的Instruments。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM