开发者

Data gets deallocated before use with ASIHTTPRequest asynchronous mode

I'm using ASIHTTPRequest in asynchronous mode in a function called from my viewDidLoad()

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

This is my requestFinished() where I do some text replacement and populate an NSArray with the data I've received from the website. It's just text, one data item per line. The NSArray (and alloc / init) in viedDidLoad().

- (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 reports 127 data items.

Now, I use the NSArray data to populate a UITableView. But, in cellForRowAtIndexPath() when I attempt to access the NSArray (arrayTidalData), for开发者_StackOverflow instance by doing a count, I get the following:

TideTimes[14696:b303] * -[__NSArrayM count]: message sent to deallocated instance 0x4e6c6a0

(I turned on NSZOMBIEEnabled = YES to get this data)

It seems the NSArray has been deallocated before I can use it. I also tried populating an NSString with the data in requestFinish() but got the same result.

Am I missing something really simple or am I doing something terribly wrong?

(It's my first time with ASIHTTPRequest)


Replace

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

with

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

It is because componentsSeparatedByString: returns autoreleased object. So it is released after method requestFinished: ends working.

And don't forget to release at the end of work (for example, dealloc).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜