1

我尝试查找其他问题,但找不到任何匹配的内容,所以这里是:

我正在尝试在表格视图中显示文本,所以我使用了这段代码:

// StockData is an object I created and it pulls information from Yahoo APIs based on 
//  a stock ticker stored in NSString *heading

    NSArray* tickerValues = [heading componentsSeparatedByString:@" "];
StockData *chosenStock = [[StockData alloc] initWithContents:[tickerValues objectAtIndex:0]];
[chosenStock getData];

// Set up the cell...
NSDictionary *tempDict = [chosenStock values];
NSArray *tempArr = [tempDict allValues];
cell.textLabel.text = [tempArr objectAtIndex:indexPath.row];
return cell;

这都在 cellForRowAtIndexPath 下

当我尝试释放 selectedStock 对象时,虽然我收到此错误:[CFDictionary release]: message sent to deallocated instance 0x434d3d0

我尝试使用 NSZombieEnabled 和 Build and Analyze 来检测问题,但到目前为止没有运气。我什至用 NSLog 评论代码的点点滴滴,但没有运气。我将在下面发布 StockData 的代码。据我所知,在我发布之前有些东西正在被释放,但我不确定如何。我在代码中唯一发布的地方是在 dealloc 方法调用下。

这是 StockData 代码:

// StockData contains all stock information pulled in through Yahoo! to be displayed

@implementation StockData

@synthesize ticker, values;

- (id) initWithContents: (NSString *)newName {
    if(self = [super init]){
        ticker = newName;
    }
    return self;
}

- (void) getData {

    NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"http://download.finance.yahoo.com/d/quotes.csv?s=%@&f=%@&e=.csv", ticker, @"chgvj1"]];
    NSError *error;
    NSURLResponse *response;
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    NSData *stockData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    if(stockData) {
        NSString *tempStr = [[NSString alloc] initWithData:stockData encoding:NSASCIIStringEncoding];       

        NSArray *receivedValuesArr = [tempStr componentsSeparatedByString:@","];
        [tempStr release];

        values = [NSDictionary dictionaryWithObjects:receivedValuesArr forKeys:[@"change, high, low, volume, market" componentsSeparatedByString:@", "]];
    } else {
        NSLog(@"Connection failed: %@", error);
    }
}

- (void)dealloc {
    [ticker release];
    [values release];   
    [super dealloc];

    NSLog(@"Release took place fine");
}

@end
4

1 回答 1

3

好吧,我可以看到一个潜在的问题......在这个片段中

   (id) initWithContents: (NSString *)newName{

  if(self = [super init]){

  ticker = newName; 
  } return self;

您没有保留股票代码,您合成股票代码,但您需要通过说 self.ticker=newName 或 ticket=[newName 保留] 来分配它,所以在这里您没有保留股票代码,而在 dealloc 中您正在释放股票代码......所以你正在过度释放股票代码,这将导致您的问题...此外,每当您释放包含股票代码字符串值的数组时,如果您尝试访问对象的股票代码属性,它将崩溃,因为您没有保留它。

于 2010-05-26T18:49:02.217 回答