如何释放NSString

发布于 2024-08-27 23:42:01 字数 1064 浏览 11 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉酒的小男人 2024-09-03 23:42:01

按照惯例,方法返回保留对象的唯一两种情况是构造函数,即 allocnew 等和对象复制方法(名称中包含 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

水晶透心 2024-09-03 23:42:01

您不应该对上述代码中的任何对象调用release

NSArray 自动释放的原因与所有其他对象自动释放的原因相同:为它们分配值的方法称为在他们返回之前autorelease。一般来说,如果方法中没有“create”或“new”一词,您可以假设方法返回自动释放的对象。这是可可的一般惯例。 (尽管第 3 方代码可能很愚蠢并且做事方式不同,所以请注意程序员)。

您只需要真正担心自己分配复制的对象即可;换句话说,将每个 alloccopyreleaseautorelease 配对。

You should not be calling release on any of the objects in the above code.

The reason the NSArray is autorelease'd is the same reason all the other objects are autorelease'd: the methods that assigned them their values called autorelease on them before they returned. In general, you can assume methods return autorelease'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 or copy yourself; in other words, pair every alloc or copy with a release or autorelease.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文