从 Sqlite 获取数据时内存泄漏

发布于 2024-11-19 17:51:50 字数 3542 浏览 3 评论 0原文

我在下面的代码中遇到内存泄漏。

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

请纠正我。

-(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);
}

Iam getting memory leaks in below code.

Is this is the proper way to handle memory?

Please do correct me.

-(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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

王权女流氓 2024-11-26 17:51:50

据我从您的代码中可以看出,泄漏是由于将 addObject 添加到数组(这会将保留计数增加一)而发生的,请确保当您删除对象并从数组中完成使用它时,您也会在那里释放它。

As far as I can tell from your code the leak happens because of addObject to the array (which increases the retain count by one) make sure that when you remove the object and are done with it from the array you release it there as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文