NSString 和 NSmutablearray 内存泄漏

发布于 2024-11-19 14:29:39 字数 3605 浏览 1 评论 0原文

当我使用 Instrument 运行时,下面的代码出现内存泄漏。

在这里我声明所有 NSMutablearrays 都是属性。

我在这里发表了评论,指出内存泄漏的具体情况。

-(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_close(database);
}
sqlite3_finalize(statement);
}

谢谢。

When I run with Instrument the below code gets memory leaking.

Here I have declared all NSMutablearrays are as properties.

I have made comments here where exactly memory is leaking.

-(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_close(database);
}
sqlite3_finalize(statement);
}

Thanks.

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

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

发布评论

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

评论(1

花期渐远 2024-11-26 14:29:39

您将在完成语句之前关闭数据库。这意味着您的语句返回的资源未正确释放。以下是文档有关sqlite3_close的内容。

应用程序必须完成所有准备好的语句并关闭所有 BLOB
在尝试之前与 sqlite3 对象关联的句柄
关闭该对象。如果在数据库上调用 sqlite3_close()
仍具有未完成的准备好的语句或 BLOB 的连接
句柄,然后返回 SQLITE_BUSY。

You are closing the database before you finalize your statement. This means that the resources returned by your statement are not properly freed. Here is what the documentation has to say about sqlite3_close.

Applications must finalize all prepared statements and close all BLOB
handles associated with the sqlite3 object prior to attempting to
close the object. If sqlite3_close() is called on a database
connection that still has outstanding prepared statements or BLOB
handles, then it returns SQLITE_BUSY.

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