NSString 发布
我有这段字符串代码,在尝试释放内存时遇到问题,我知道只有那些释放它的人才会初始化,而不是自动释放,但我在字符串“end”和 nSum 释放方面遇到了问题。
NSString *urlBase = [[NSString alloc] initWithFormat:@"http://service.svc/"];
NSString *op = [[NSString alloc] initWithFormat:@"op1"];
NSString * final = [urlBase stringByAppendingFormat:op];
NSString * nSum = sumTextfield.text;
final = [final stringByAppendingFormat:nSum];
//release
[ urlBase release ];
[ op release ];
//[final release]; error
//[final autorelease]; error
谢谢你的帮助。
更新:
- (IBAction)mostrarOpciones {
// code (UP)
}
I have this code of string and I have problems trying to free up memory, I have understood that only those who release it initializes and is not autorelease but I had problems with the string "end", and as nSum release.
NSString *urlBase = [[NSString alloc] initWithFormat:@"http://service.svc/"];
NSString *op = [[NSString alloc] initWithFormat:@"op1"];
NSString * final = [urlBase stringByAppendingFormat:op];
NSString * nSum = sumTextfield.text;
final = [final stringByAppendingFormat:nSum];
//release
[ urlBase release ];
[ op release ];
//[final release]; error
//[final autorelease]; error
thank for you help.
UPDATE:
- (IBAction)mostrarOpciones {
// code (UP)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用以 init、new、copy 或 mutableCopy 开头的方法创建对象,那么您拥有该对象,并负责在使用完该对象后释放它(或自动释放它)。如果您使用任何其他方法创建对象,该对象会自动释放,并且您不需要释放它。在这种情况下,如果您想保留该对象,实际上需要保留该对象。 Apple 有一个内存管理编程指南 包括所有这些规则。
您发布的代码实际上是正确的。您需要释放
urlBase
和op
,因为您使用以 init 开头的方法创建了它们(在本例中为initWithFormat:
)。final
和nSum
已为您自动发布。 Final 是由不以 init、new、copy 或 mutableCopy 开头的方法创建的(在本例中为工厂方法stringByAppendingFormat:
)。 nSum 由名为text
的方法返回,您可以假设 sumTextField“拥有”它或在将其返回给您之前已自动释放它,因此您不负责释放它。If you create an object using a method that begins with init, new, copy, or mutableCopy, then you own that object and are responsible for releasing it (or autoreleasing it) when you're done with it. If you create an object using any other method, that object is autoreleased, and you don't need to release it. In that case, you actually need to retain the object if you want to keep it around. Apple has a Memory Management Programming Guide that includes all these rules.
The code you've posted is actually correct. You need to release
urlBase
andop
because you created them using a method beginning with init (initWithFormat:
in this case).final
andnSum
are already autoreleased for you. final was created by a method that doesn't begin with init, new, copy or mutableCopy (in this case, the factory methodstringByAppendingFormat:
). nSum was returned by a method calledtext
, and you can assume that sumTextField "owns" it or has autoreleased it before returning it to you, and so you're not responsible for releasing it.你不能释放你没有分配的NSString。由于您的两个变量没有分配,因此不需要释放它们。
you cannot release NSString which you did not allocate. Since your two variables are not allocated, they need not to be released.