views:

209

answers:

1

I tried looking up other issues but couldn't find anything to match so here goes:

I'm trying to display text in the table view so I use this bit of code:

// 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;

This is all under cellForRowAtIndexPath

When I try to release the chosenStock object though I get this error: [CFDictionary release]: message sent to deallocated instance 0x434d3d0

Ive tried using NSZombieEnabled and Build and Analyze to detect problems but no luck thus far. Ive even gone so far as to comment bits and pieces of the code with NSLog but no luck. I'll post the code for StockData below this. As far as I can figure something is getting deallocated before I do the release but I'm not sure how. The only place I've got release in my code is under dealloc method call.

Here's the StockData code:

// 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
+2  A: 

Well i can see a potential problem...in this snippet

   (id) initWithContents: (NSString *)newName{

  if(self = [super init]){

  ticker = newName; 
  } return self;

You are not retaining ticker, u syntheisze ticker, but u need to assign it by saying self.ticker=newName or ticket=[newName retain], so here you are not retaining ticker and in dealloc you are releasing ticker...so you are overreleasing ticker which wil lcause your problem...Also whenever u release that array thats holding your ticker string value, if u try to access the ticker proerty of the object, it will crash since u have not retained it..

Daniel
Good catch, but I would recommend against using `self.ticker = newName` in your `init` method. It is safer to use `ticker = [newName copy]`. This practice is recommended in case you every change the property accessor for `ticker` into something more complex. I'll find a link to the full argument in a second.
e.James
Hey thanks for the quick response! That worked!!I have one question though. In the property for ticker I set it to (nonatomic, retain). I thought that would have done the trick for the value it got from newName. Are you saying that I needed to retain the newName object that ticker points to? Setting the property for ticker to retain doesn't do that?
Kirn
Take a peek at my answer to another question for more details: http://stackoverflow.com/questions/1394360/bad-access-error-even-though-property-is-set-to-retain
e.James
AHHhhhh thanks! I love this site!
Kirn
Here's the link I promised: http://stackoverflow.com/questions/192721/why-shouldnt-i-use-obective-c-2-0-accessors-in-init-dealloc
e.James
when u declare a property and synthesize it you have to access the generated setter by saying self.propertyname in order for it to retain itself...yes e.james it is better to use the synthesized property, i was just showing alternatives.
Daniel
@Kirn: Happy to help! Just a quick note on site ettiqute: since Daniel provided you with the correct answer, it is customary to upvote his answer as well as mark it as accepted. This is by no means mandatory, but it is the way things are usually done. Have fun with the coding! `:)`
e.James
I keep trying to do the up vote but I think I need a certain reputation (points?) to do it. Thanks a lot guys.
Kirn