Note that there are some explanatory texts on larger screens.

plurals
  1. POIPhone autoreleasing a returned NSMutableArray
    primarykey
    data
    text
    <p>I'm still relatively new to iPhone development but thought I understood the basic principals of memory management. I have a class method that returns an NSMutableArray, I'm calling alloc and init on the object therefore know I'm responsible for releasing it. However because I'm returning the array I assumed I was supposed to use autorelease when creating the object instead of releasing it.</p> <pre><code>+(NSMutableArray *)getStations:(int)stationType { if(database == nil){ [self openDataBase]; } // Create a temporary array to hold the returned objects NSMutableArray *holder = [[[NSMutableArray alloc] init] autorelease]; // Check if the statement has been defined if(select4 == nil) { const char *sql = "SELECT station_id, station_name, AVG(test_percent) FROM stations LEFT JOIN tests USING (station_id) WHERE station_type = ? GROUP BY station_id ORDER BY station_name ASC"; if(sqlite3_prepare_v2(database, sql, -1, &amp;select4, NULL) != SQLITE_OK){ NSLog(@"Error while creating detail view statement. '%s'", sqlite3_errmsg(database)); } } sqlite3_bind_int(select4, 1, stationType); // Check if the statement executed correctly while(sqlite3_step(select4) == SQLITE_ROW) { NSInteger primaryKey = sqlite3_column_int(select4, 0); Tests *station = [[Tests alloc] initWithPrimaryKey:primaryKey]; station.station_name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(select4, 1)]; station.average_score = [NSNumber numberWithDouble:sqlite3_column_double(select4, 2)]; [holder addObject:station]; [station release]; } // Reset the detail statement. sqlite3_reset(select4); // Return the holder array return holder; } </code></pre> <p>There's the basic code - XCode no longer indicates a potential memory leak but it crashes everytime that code executes saying message sent to deallocated instance. Any help would be appreciated I've spent ages googling and can't see what's wrong with the code. I did find this thread but it doesn't appear to be the answer to my question - <a href="https://stackoverflow.com/questions/2452959/crash-happens-when-nsmutablearray-is-returned">crash happens when NSMutableArray is returned?</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload