2010-05-26 68 views
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

當我嘗試釋放chosenStock對象,雖然我得到這個錯誤:[CFDictionary發佈]:發送到釋放實例0x434d3d0

消息

我試過使用NSZombieEnabled和構建和分析來檢測問題,但到目前爲止沒有運氣。我甚至已經用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 

回答

3

好吧,我可以看到一個潛在的問題......在這個片段中

(id) initWithContents: (NSString *)newName{ 

    if(self = [super init]){ 

    ticker = newName; 
    } return self; 

你是不是保留股票,U syntheisze股票,但你需要通過說self.ticker = newName或ticket = [newName retain]來分配它,所以在這裏你不保留股票代碼,並在dealloc中發佈股票...所以你正在過度釋放股票,這將導致你的問題......還有,無論何時你釋放那個持有你的股票字符串值的數組,如果你嘗試訪問t但是我會建議不要在你的`init`方法中使用`self.ticker = newName`,否則它會崩潰,因爲你還沒有保留它..

+0

好,使用'ticker = [newName copy]'更安全。如果您將'ticker'的屬性訪問器更改爲更復雜的東西,則建議使用此練習。我會在一秒鐘內找到完整參數的鏈接。 – 2010-05-26 18:54:10

+0

嗨,感謝您的快速回復!這工作! 雖然我有一個問題。在ticker的屬性中,我將它設置爲(nonatomic,retain)。我認爲這會爲從newName獲得的值做訣竅。你是否說我需要保留ticker指向的newName對象?設置股票的財產保留不會這樣做? – Kirn 2010-05-26 18:57:14

+0

查看我對另一個問題的回答以獲取更多詳細信息:http://stackoverflow.com/questions/1394360/bad-access-error-even-though-property-is-set-to-retain – 2010-05-26 19:02:23

相關問題