如何释放NSString
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [path objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"DB"];
NSString *fileName = [newWordbookName stringByAppendingString:@".csv"];
NSString *fullPath = [databasePath stringByAppendingPathComponent:fileName];
[[NSFileManager defaultManager] createFileAtPath:fullPath contents:nil attributes:nil];
[databasePath release];
//[fileName release]; Error!
//[fullPath release]; Error!
//NSLog(@"#1 :databasePath: %d",[databasePath retainCount]);
//NSLog(@"#1 :fileName: %d",[fileName retainCount]);
//NSLog(@"#1 :fullPath: %d",[fullPath retainCount]);
我正在使用这段代码并想释放 NSString* .. 所以,我声明 NSString 的 fileName、fullPath 和 databasePath。 数据库已释放,但文件名、完整路径未释放。我不知道为什么会这样。
我知道 NSArray 是自动释放的。但是documentsDirectory是自动释放的吗? (newWordbookName是nsstring类型)
我希望翻阅一篇关于iPhone内存管理的文档。
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [path objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"DB"];
NSString *fileName = [newWordbookName stringByAppendingString:@".csv"];
NSString *fullPath = [databasePath stringByAppendingPathComponent:fileName];
[[NSFileManager defaultManager] createFileAtPath:fullPath contents:nil attributes:nil];
[databasePath release];
//[fileName release]; Error!
//[fullPath release]; Error!
//NSLog(@"#1 :databasePath: %d",[databasePath retainCount]);
//NSLog(@"#1 :fileName: %d",[fileName retainCount]);
//NSLog(@"#1 :fullPath: %d",[fullPath retainCount]);
I'm using this code and want to release NSString* ..
so, I declare fileName, fullPath, and databasePath of NSString.
database is released but fileName, fullpath doesn't release. I don't know why it happens.
I know that NSArray is Autoreleased. But is documentsDirectory autoreleased?
(newWordbookName is nsstring type)
I hope that I look through a document about iPhone memory management.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按照惯例,方法返回保留对象的唯一两种情况是构造函数,即 alloc、new 等和对象复制方法(名称中包含 copy)。
在所有其他情况下,除非文档中另有明确说明,否则该对象预计会自动释放。
这是完整的内存管理文档:
Cocoa 内存管理
By convention the only two cases when a method returns a retained object are constructors i.e. alloc, new etc. and object-copying methods (containing copy in their name).
In all other cases the object is expected to be autoreleased, unless explicitly stated otherwise in the documentation.
This is the complete memory management documentation:
Cocoa Memory Management
您不应该对上述代码中的任何对象调用
release
。NSArray 自动释放的原因与所有其他对象自动释放的原因相同:为它们分配值的方法称为在他们返回之前
autorelease
。一般来说,如果方法中没有“create”或“new”一词,您可以假设方法返回自动释放的对象。这是可可的一般惯例。 (尽管第 3 方代码可能很愚蠢并且做事方式不同,所以请注意程序员)。您只需要真正担心自己
分配
或复制
的对象即可;换句话说,将每个alloc
或copy
与release
或autorelease
配对。You should not be calling
release
on any of the objects in the above code.The reason the
NSArray
isautorelease
'd is the same reason all the other objects areautorelease
'd: the methods that assigned them their values calledautorelease
on them before they returned. In general, you can assume methods returnautorelease
'd objects if they do not have the word "create" or "new" in them. That is the general Cocoa convention. (Although 3rd party code may be goofy and do things differently, so caveat programmer).You only really need to worry about objects you
alloc
orcopy
yourself; in other words, pair everyalloc
orcopy
with arelease
orautorelease
.