简体   繁体   English

NSString基础知识 - 内存 - 保留 - 复制

[英]NSString basics - memory - retain - copy

Here is my code: 这是我的代码:

NSString *xyz=[NSString stringWithFormat:@"%i %@",10,@"Sagar"];

Now I am taking other string, as follows. 现在我正在采取其他字符串,如下所示。

NSString *x2=[xyz copy];

I don't know exactly what will happen here? 我不知道到底会发生什么? Is it something like, x2 has the ref of xyz's ref? 是这样的,x2有xyz的ref的参考吗?

NSString *x3=[xyz retain];

What will happen here, x3 has a new memory having copied string or [xyz copy] does that? 这里会发生什么,x3有一个新的内存有复制的字符串或[xyz copy]吗?

Now, how to remove all these three strings from memory? 现在,如何从内存中删除所有这三个字符串?

NSString *xyz=[NSString stringWithFormat:@"%i %@",10,@"Sagar"];

This will create autoreleased instance of NSString - it will be released when the autorelease pool is drained (typically on the next run loop). 这将创建自动释放的NSString实例 - 它将在自动释放池耗尽时释放(通常在下一个运行循环中)。

NSString *x2 = [xyz copy];

In theory -copy message will create a new instance of the object with retain count 1 (that is you must release it somewhere), but as NSString object is immutable then [xyz copy] will be optimized to [xyz retain] and thus it will point to the same instance. 在理论上-copy消息将创建一个具有保留计数1的对象的新实例(即你必须在某处释放它),但由于NSString对象是不可变的,那么[xyz copy]将被优化为[xyz retain],因此它将指向同一个实例。

NSString *x3=[xyz retain];

x3 will point to the same instance as xyz (and x2), and its retain count will be incremented - you must release your object somewhere. x3将指向与xyz(和x2)相同的实例,并且其保留计数将递增 - 您必须在某处释放您的对象。

Now, how to remove all these three strings from memory? 现在,如何从内存中删除所有这三个字符串?

Make sure that you pair all retain (copy) messages with release and memory will be freed. 确保将所有保留(复制)消息与释放和内存配对将被释放。
Read Objective-c memory management guide for more details. 阅读Objective-c内存管理指南以获取更多详细信息。

In situation like this it is especially helpful to familiarize yourself with the message naming conventions/rules associated with memory management in objective-c and cocoa (and related frameworks): 在这种情况下,熟悉与objective-c和cocoa(以及相关框架)中的内存管理相关的消息命名约定/规则特别有用:

You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message. 如果使用名称以“alloc”或“new”开头或包含“copy”(例如,alloc,newObject或mutableCopy)的方法创建对象,或者向其发送保留消息,则获取对象的所有权。 You are responsible for relinquishing ownership of objects you own using release or autorelease. 您有责任使用release或autorelease放弃您拥有的对象的所有权。 Any other time you receive an object, you must not release it. 在收到对象的任何其他时间,您不得释放它。 ( Memory Management Programming Guide for Cocoa ) Cocoa内存管理编程指南

consequently, you can assume, that every object that you ever receive from a message that is not named according to the scheme laid out above is either autoreleased or taken care of by some other means (it may be a shared object managed by some other object etc.) 因此,您可以假设,您从根据上述方案未命名的消息中收到的每个对象都是自动释放或通过其他方式处理(它可能是由其他对象管理的共享对象)等等。)

If you just keep this in mind, your questions can be answered quickly: 如果您记住这一点,您的问题可以快速得到解答:

  1. You receive the NSString *xyz from a message whose name does not match the scheme described in the rule above (not alloc, not new, not copy, not retain). 您从一条消息中收到NSString * xyz,该消息的名称与上述规则中描述的方案不匹配(不是alloc,不是new,不是copy,不是retain)。 You must not release it. 不能发布它。

  2. You receive the NSString *x2 from a message named copy. 您从名为copy的消息中收到NSString * x2。 You must release it 必须释放它

  3. You receive the NSString *x3 from a message named retain. 您从名为retain的消息中收到NSString * x3。 You must release it. 必须释放它。

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

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