内存泄漏从Sqlite获取数据时

时间:2011-07-14 06:42:23

标签: sqlite nsstring

我在下面的代码中发现了内存泄漏。

这是处理内存的正确方法吗?

请纠正我。

-(void)getholidays
{   
   if (idarray!=nil) {
   [idarray release];
    idarray=nil;
     }
 idarray=[[NSMutableArray alloc]init];

   if (Countryarray!=nil) {
    [Countryarray release];
    Countryarray=nil;
     }
   Countryarray =[[NSMutableArray alloc] init];
   if (Holidaynamearray!=nil) {
    [Holidaynamearray release];
    Holidaynamearray=nil;
     }
   Holidaynamearray =[[NSMutableArray alloc] init];
   if (Datearray!=nil) {
    [Datearray release];
    Datearray=nil;
   }Datearray =[[NSMutableArray alloc] init];
    if (Dayarray!=nil) {
    [Dayarray release];
    Dayarray=nil;
   }Dayarray =[[NSMutableArray alloc] init];
   if (Favoritearray!=nil) {
    [Favoritearray release];
    Favoritearray=nil;
   }

   Favoritearray =[[NSMutableArray alloc] init];

   NSString *destinationPath = [self getdestinationPath];

   const char *dbpath = [destinationPath UTF8String];
    sqlite3_stmt    *statement;

    if (sqlite3_open(dbpath, &database) == SQLITE_OK)

    {
    NSString *querySQL;


    NSDate *today = [NSDate date];
    NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateFormat:@"MMMM-dd-yyyy"];
    NSString *Todaystrng = [formatter stringFromDate:today];

    NSLog(@"today date %@",Todaystrng);

    querySQL=[NSString stringWithFormat:@"SELECT * FROM Holiday_Table WHERE CountryName in (SELECT Country_Name from Country WHERE Country_Selected =1) ORDER BY  Date ASC "];



    const char *query_stmt = [querySQL UTF8String];

    if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK)  
    {
        NSLog(@"success");
        while (sqlite3_step(statement) == SQLITE_ROW)
        {

            NSString *idstringfromdb=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)]; 
            NSString *countrynamestringfromdb=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
            NSString *holidaynamestringfromdb=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
            NSString *datestringfromdb=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];

            //Below line gets leaking   
              NSString *daystringfromdb=[[NSString alloc] initWithUTF8String:(const char *)  sqlite3_column_text(statement, 4)];
                  //Belowline gets leaking
          NSString *favoritestringfromdb=[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 5)];


            [idarray addObject:idstringfromdb];
            [idstringfromdb release];
            idstringfromdb=nil;
            [Countryarray addObject:countrynamestringfromdb];
            [countrynamestringfromdb release];
            countrynamestringfromdb=nil;
            [Holidaynamearray addObject:holidaynamestringfromdb];
            [holidaynamestringfromdb release];
            holidaynamestringfromdb=nil;
            [Datearray addObject:datestringfromdb];
            [datestringfromdb release];
            datestringfromdb=nil;

            [Dayarray addObject:daystringfromdb];
            [daystringfromdb release];
            daystringfromdb=nil;

            [Favoritearray addObject:favoritestringfromdb];
            [favoritestringfromdb release];
            favoritestringfromdb=nil;
        }
    }
    sqlite3_finalize(statement);
}
sqlite3_close(database);
}

1 个答案:

答案 0 :(得分:0)

据我所知,你的代码泄漏是因为addObject到数组(这会使保留计数增加一个)确保当你删除对象并从数组中完成它时你会释放它同样。