附加到 NSString 时正确的内存管理
我已经分配了一个 NSString
对象。我想在应用程序的整个生命周期中偶尔附加到该字符串 我对如何正确处理下面示例中的内存感到困惑。谢谢。
- 我是否需要保留每次附加的字符串,或者是否可以附加属于自动释放池的字符串,如
stringByAppendingString:
返回? - 每次执行追加操作时,我是否都会泄漏之前分配给
my_string
的内存?
代码:
NSString* my_string = [[NSString alloc] initWithString:@"Initial string"];
NSString* something = [NSString stringWithFormat:@"%@", @" with something"];
my_string = [[my_string stringByAppendingString:something] retain];
NSString* something_else = [NSString stringWithFormat:@"%@", @" and something_else"];
my_string = [[my_string stringByAppendingString:something_else] retain];
[my_string release];
I have an NSString
object that I've allocated. I want to append to this string occasionally throughout the lifetime of the app
I am confused about how to properly handle the memory in the example below. Thanks.
- Do I need to retain the string that I am appending each time, or is it OK to append with a string that belongs to the autorelease pool, as
stringByAppendingString:
returns? - Am I leaking the memory that was previously assigned to
my_string
each time I do an append?
Code:
NSString* my_string = [[NSString alloc] initWithString:@"Initial string"];
NSString* something = [NSString stringWithFormat:@"%@", @" with something"];
my_string = [[my_string stringByAppendingString:something] retain];
NSString* something_else = [NSString stringWithFormat:@"%@", @" and something_else"];
my_string = [[my_string stringByAppendingString:something_else] retain];
[my_string release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不是自动释放的(因为它是使用 init... 方法创建的,这是约定),它的保留计数为 1。
是自动释放的。
只要您期望它存在就可以。但是在您的代码中:
您有泄漏。您应该在重新分配 my_string 之前自动释放它。所以最后一行应该是:
否则你将永远失去指向你应该释放的对象的指针。当你保留结果时,你有责任释放它,所以你在最后一行之前也有泄漏,这应该是
is not autoreleased (as it is created with an init... method, this is a convention), it has a retainCount of 1.
is autoreleased.
As long as you would expect it to live for yes. However in your code:
you have a leak. You should autorelease my_string before re-assigning it. So the last line should be:
otherwise you have forever lost the pointer to the object you are meant to have released. As you retain the result, you are responsible for releasing it so you also have a leak in the line before last which should be
首先要记住的是
NSString
是不可变的。无论您是分配、使用格式创建还是追加,您总是会返回一个新字符串对象。那么让我们来分解一下:所以:
是的。附加一个自动释放的字符串就可以了。在运行循环结束之前(至少是在该方法退出之后),不会对自动释放的对象调用
release
。是的,每次执行追加时,您都会泄漏之前分配给
my_string
的对象...但导致泄漏的不是追加,而是分配。您可以将nil
分配给my_string
,并且如果您不release
,用于指向的保留对象my_string
仍然会泄漏code> 或autorelease
首先。The primary thing to keep in mind is that
NSString
is immutable. Whether you alloc, create with format, or append, you're always getting a new string object returned. So let's break this down:So:
Yes. It's ok to append an autoreleased string.
release
is not called on an autoreleased object until the end of the run loop, which, at the very least, is after this method exits.Yes, you are leaking the objects that were previously assigned to
my_string
each time you do an append... but it's not the append that's causing the leak, it's the assignment. You could assignnil
tomy_string
and the retained objectmy_string
used to point to would still leak if you didn'trelease
orautorelease
it first.在第二个作业中,您将丢失第一个
stringByAppendingString
调用的保留引用,因此:stringByAppendingString
都会返回一个新的自动释放字符串。如果您暂时使用该值,请勿保留它。In the 2nd assignment you're losing the retained reference of the first
stringByAppendingString
call, so:stringByAppendingString
returns a new autoreleased string. Don't retain it if you're using the value temporarily.