title: sqlite3 memory leaks
body: <p>I'm developing an app with makes an heavy use of sqlite3 and I'm facing a huge memory leaks problem.</p>
  
  <p>Every time I call the method which makes the select I get memory leaks when I add the object to the array.</p>
  
  <pre><code>    - (void) makeSitesSelect:(NSString *)dbPath:(NSString *)theSelect {
      allSitesSelect = [[NSMutableArray alloc] init];
      sqlite3 *database;
      if (sqlite3_open([dbPath UTF8String], &amp;database) == SQLITE_OK) {
          const char *sql = [theSelect cStringUsingEncoding:NSASCIIStringEncoding];
          sqlite3_stmt *selectstmt;
  
          if(sqlite3_prepare_v2(database, sql, -1, &amp;selectstmt, NULL) == SQLITE_OK) {
              while(sqlite3_step(selectstmt) == SQLITE_ROW) {
                  NSMutableArray *selResult = [[NSMutableArray alloc] init]; // &lt;---- Memory leak
                  NSInteger selprimaryKey = sqlite3_column_int(selectstmt, 0);
                  [selResult addObject:[NSString stringWithFormat:@"%i", selprimaryKey]]; //&lt;--- memory leak
  
                  const unsigned char *chsiteSite = sqlite3_column_text(selectstmt, 1);
                  const unsigned char *chsiteCity = sqlite3_column_text(selectstmt, 2);
                  const unsigned char *chsiteCountry = sqlite3_column_text(selectstmt, 3);
                  const unsigned char *chsiteGPS = sqlite3_column_text(selectstmt, 4);
  
                  if (chsiteSite != NULL) {
                      [selResult addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)]]; //&lt;--- memory leak
                  }
                  else {
                      [selResult addObject:@""];
                  }
  
                  if (chsiteCity != NULL) {
                      [selResult addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)]]; //&lt;---- memory leak
                  }
                  else {
                      [selResult addObject:@""];
                  }
                  if (chsiteCountry != NULL) {
                      [selResult addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)]]; //&lt;---- memory leak
                  }
                  else {
                      [selResult addObject:@""];
                  }
                  if (chsiteGPS != NULL) {
                      [selResult addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)]]; //&lt;---- memory leak
                  }
                  else {
                      [selResult addObject:@""];
                  }
                  [allSitesSelect addObject:selResult];
                  [selResult release];
              }
              sqlite3_finalize(selectstmt);
          }
          sqlite3_close(database);
      }
      else
          sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
  }
  </code></pre>
  
  <p>Any help is appreciated.</p>
  
  <p>Thanks,
  Max</p>