tableview中的deallocated memory:发送到deallocated实例的消息

时间:2010-05-26 18:40:03

标签: iphone count memory-management tableview

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

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

// 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]:消息发送到解除分配的实例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

1 个答案:

答案 0 :(得分:3)

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

   (id) initWithContents: (NSString *)newName{

  if(self = [super init]){

  ticker = newName; 
  } return self;

你没有保留自动收报机,你合成自动收报机,但你需要通过说self.ticker = newName或ticket = [newName retain]来分配它,所以在这里你没有保留自动收报机,并且在dealloc中你发布了自动收报机.. 。所以你过度发布了会导致你的问题的自动收报机......每当你发布那个持有你的自动收报机字符串值的数组时,如果你试图访问该对象的自动收报机,那么它会崩溃,因为你没有保留它。