views:

171

answers:

2

I have declared a NSMutableArray and I populated it with information from the database. The table displays information well. But when I scroll down, the application exits. Seems like its loosing the pointer to the array.

Here is the code for declaration of the array:

@interface RootViewController : UITableViewController <CLLocationManagerDelegate> {
 sqlite3 *database;

 NSMutableArray *storeList;
 CLLocationManager *locationManager;
 CLLocation *startingPoint;

}

@property (nonatomic, retain) NSMutableArray *storeList;
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) CLLocation *startingPoint;    

- (void) createCopyOfDatabaseIfNeeded;
- (void) initializeStoreList;
- (void) getDistanceFromUserLocation;

Here I am initializing the array with object of type StoreInfo:

- (void) initializeStoreList{
 self.storeList = [[NSMutableArray alloc] init];

 //database is stored in the application bundle.
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:kFileName];

 if (sqlite3_open([dbPath UTF8String], &database)== SQLITE_OK) {
  const char *sql = "select id, storename, ratings, lattitude, longitude from storeinformation";
  sqlite3_stmt *statement;

  if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
   while (sqlite3_step(statement) == SQLITE_ROW) {
    NSInteger *_pk = (NSInteger *) sqlite3_column_int(statement, 0);
    NSString *_storeName =  [NSString stringWithUTF8String:(char*) sqlite3_column_text(statement, 1)];
    NSString *_ratings = [NSString stringWithUTF8String:(char*) sqlite3_column_text(statement, 2)];
    double _lattitude = [[NSString stringWithUTF8String:(char*) sqlite3_column_text(statement, 3)] doubleValue];
    double _longitude = [[NSString stringWithUTF8String:(char*) sqlite3_column_text(statement, 4)] doubleValue];

    StoreInfo *si = [[StoreInfo alloc] initWithBasicInformation:_pk storeName:_storeName ratings:_ratings lattitude:_lattitude longitude:_longitude];
    [self.storeList addObject:si];
    [si release];

   }
  }

  sqlite3_finalize(statement);
 } else {
  sqlite3_close(database);
  NSAssert1(0,@"Failed to open the database with message '%s'.", sqlite3_errmsg(database));
 }

}

here is the constructor for StoreInfo object

-(id)initWithBasicInformation:(NSInteger *)_pk storeName:(NSString *) _storeName ratings:(NSString *) _ratings lattitude:(double) _lattitude longitude:(double) _longitude;
{
     if (self = [super init]) {
  self.primaryKey = _pk;
  self.storeName = _storeName;
  self.ratings = _ratings;
  self.lattitude = _lattitude;
  self.longitude = _longitude;
 }
 return self;
}

Here is the code for displaying the cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

 // Configure the cell.
 StoreInfo *si = (StoreInfo *)[self.storeList objectAtIndex:indexPath.row];
 cell.textLabel.text = si.storeName;
    return cell;
}

The table displays alright first. But when I scroll down, this even gets fired and somehow it is not able to find reference to the si.storeName.

I have spent hours trying to debug the issue. Any help is greatly appreciated.

A: 

First of all, how have you defined the property for the problematic field? Is it retain?

Secondly, Can you access any other property in si?

And finally, I see that there is a memory leak in self.storeList = [[NSMutableArray alloc] init]; - the object is retained twice (in init and in the property setter)...

Michael Kessler
The property field was not defined as retain. After defining as retain, it fixed the issue. THANK YOU THANK YOU THANK YOU!!
Shirish
A: 

Hello!

I have a similar problem. The code is the same, but my application exits when you select a element of the UITableView for the second time. Anybody know the problem, please?

Regards.

Guilermo Álvarez