【问题标题】:Data gets deallocated before use with ASIHTTPRequest asynchronous mode数据在使用 ASIHTTPRequest 异步模式之前被释放
【发布时间】:2011-09-04 09:56:27
【问题描述】:

我在从我的 viewDidLoad() 调用的函数中以异步模式使用 ASIHTTPRequest

NSURL *url = [NSURL URLWithString:@"http://somewebsite.com/data.txt"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];

这是我的 requestFinished() ,我在其中进行一些文本替换并使用从网站收到的数据填充 NSArray。它只是文本,每行一个数据项。 viedDidLoad() 中的 NSArray(和 alloc / init)。

- (void)requestFinished:(ASIHTTPRequest *)request
{
   NSString *sTemp = [request responseString]; 
   sTemp = [sTemp stringByReplacingOccurrencesOfString:@"\n" withString:@","];          
   arrayTidalData = [sTemp componentsSeparatedByString:@","]; 
   NSLog(@"Loaded %d items into the array",[arrayTidalData count]);
   [tableTideTimes reloadData];
}

NSLog 报告 127 个数据项。

现在,我使用 NSArray 数据填充 UITableView。 但是,在 cellForRowAtIndexPath() 中,当我尝试访问 NSArray (arrayTidalData) 时,例如通过计数,我得到以下信息:

TideTimes[14696:b303] * -[__NSArrayM count]:消息发送到释放的实例 0x4e6c6a0

(我打开 NSZOMBIEEnabled = YES 来获取此数据)

似乎 NSArray 在我可以使用之前已被释放。我还尝试使用 requestFinish() 中的数据填充 NSString,但得到了相同的结果。

我是错过了一些非常简单的事情还是我做错了什么?

(这是我第一次使用 ASIHTTPRequest)

【问题讨论】:

    标签: iphone objective-c asihttprequest


    【解决方案1】:

    替换

    arrayTidalData = [sTemp componentsSeparatedByString:@","];
    

    与

    arrayTidalData = [[sTemp componentsSeparatedByString:@","] retain];
    

    这是因为componentsSeparatedByString: 返回autoreleased 对象。所以在方法requestFinished: 结束工作后是released。

    并且不要忘记在工作结束时发布(例如,dealloc)。

    【讨论】:

    • 谢谢,我根本没想过要添加保留。太棒了!
    猜你喜欢
    • 2016-07-11
    • 2019-01-08
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    相关资源
    最近更新 更多