【发布时间】:2011-01-01 13:32:35
【问题描述】:
如果我有这个代码
NSString *postData = [@"foo=" stringByAppendingString:fooText.text];
...
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
...
[postData release]; //this causes crash
[request release]; //this causes crash
现在我知道这是expected behavior according to Apple's documents。现在,如果我删除发布代码,则不会发生崩溃,但我发现内存泄漏无论如何*request。所以我重写了代码
NSString *postData;
//postData = [NSString alloc]; // this line commented out since OP
postData = [@"foo=" stringByAppendingString:fooText.text];
...
NSMutableURLRequest *request;
request = [NSMutableURLRequest alloc];
request = [request initWithURL:url];
...
[postData release]; //this still crashes #
[request release]; //this works fine
我真的不明白为什么它会在 # 崩溃。这里有什么推荐的最佳实践吗?我想我一定遗漏了一些东西,因为我经常看到“速记”方法(顶部)发布(例如 Kochan,Objective-C 编程),但 Apple 文档说这是错误的。
【问题讨论】: