iphone sqlite3对象分配内存增加但没有泄漏

发布于 2024-07-24 09:59:34 字数 2510 浏览 5 评论 0原文

我一直在试图找出原因。 sqy 每次调用此函数时,我的对象分配都会保持正确状态,仪器报告没有泄漏,但我得到了很多对象,因为

sqlite3_exec --> sqlite3Prepare --> sqlite3Parser --> yy_reduce --> malloc  & also a whole bunch from 

& from

sqlite3Step --> sqlite3VdbeExec --> sqlite3BtreeInsert --> malloc

我尝试按照此处发布的建议来解决它: http://www.iphonedevsdk.com/forum/iphone-sdk -development/7092-sqlite3-database-gobbling-up-memory.html 但无法修复它

任何帮助表示赞赏,我的代码如下

+(void)getDesignationsInLibrary:(NSString *)library
{
NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];    
NSString *dbName = @"s8.sqlite";



NSArray *documentPaths = \
NSSearchPathForDirectoriesInDomains \
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = \
[documentPaths objectAtIndex:0];
NSString *databasePath = \
[documentsDir stringByAppendingPathComponent:dbName];

[[DT sharedDT].designationsInLibrary removeAllObjects];

NSString *sqlString;

for(int i=0;i<[[DT sharedDT].typesInLibrary count];i++)
{   

 if(sqlite3_open([databasePath UTF8String], &db)==SQLITE_OK)
 {
     if (sqlite3_exec(db, "PRAGMA CACHE_SIZE=50;", NULL, NULL, NULL) != SQLITE_OK) {
         NSAssert1(0, @"Error: failed to set cache size with message '%s'.", sqlite3_errmsg(db));
     }
     NSMutableString *lib=[NSMutableString stringWithString:library];

     [lib appendString:@"-"];
     [lib appendString:[[DT sharedDT].typesInLibrary objectAtIndex:i]];

     if([DT sharedDT].sortedBy==@"AISC Default")
     {
         sqlString = [NSString stringWithFormat:@"select DESIGNATION from \"%@\";",lib];
     }
     else
     {
         sqlString = [NSString stringWithFormat:@"select DESIGNATION from \"%@\" order by cast(%@ as numeric) %@;",lib, [DT sharedDT].sortedBy, [DT sharedDT].sortAscDesc]; 
     }

     const char *sql = [sqlString cStringUsingEncoding:NSASCIIStringEncoding];
     sqlite3_stmt *selectstmt;

     if(sqlite3_prepare_v2(db,sql,-1,&selectstmt, NULL)==SQLITE_OK)
     {
            while(sqlite3_step(selectstmt)==SQLITE_ROW)
            {
                [[DT sharedDT].designationsInLibrary addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,0)]];
            }
            sqlite3_finalize(selectstmt);
         selectstmt=nil;
     }
 }

}   
sqlite3_close(db);
[localPool release];
}

i've been trying to figure out wh. sqy my object allocation keeps rigth up every time i call this function, Instruments reports no leaks but I get a heck of a lot of object coming from

sqlite3_exec --> sqlite3Prepare --> sqlite3Parser --> yy_reduce --> malloc  & also a whole bunch from 

& from

sqlite3Step --> sqlite3VdbeExec --> sqlite3BtreeInsert --> malloc

I tried solving it by following the suggestions posted here: http://www.iphonedevsdk.com/forum/iphone-sdk-development/7092-sqlite3-database-gobbling-up-memory.html but haven't been able to fix it

ANY HELP is appreciated, my code is below

+(void)getDesignationsInLibrary:(NSString *)library
{
NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];    
NSString *dbName = @"s8.sqlite";



NSArray *documentPaths = \
NSSearchPathForDirectoriesInDomains \
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = \
[documentPaths objectAtIndex:0];
NSString *databasePath = \
[documentsDir stringByAppendingPathComponent:dbName];

[[DT sharedDT].designationsInLibrary removeAllObjects];

NSString *sqlString;

for(int i=0;i<[[DT sharedDT].typesInLibrary count];i++)
{   

 if(sqlite3_open([databasePath UTF8String], &db)==SQLITE_OK)
 {
     if (sqlite3_exec(db, "PRAGMA CACHE_SIZE=50;", NULL, NULL, NULL) != SQLITE_OK) {
         NSAssert1(0, @"Error: failed to set cache size with message '%s'.", sqlite3_errmsg(db));
     }
     NSMutableString *lib=[NSMutableString stringWithString:library];

     [lib appendString:@"-"];
     [lib appendString:[[DT sharedDT].typesInLibrary objectAtIndex:i]];

     if([DT sharedDT].sortedBy==@"AISC Default")
     {
         sqlString = [NSString stringWithFormat:@"select DESIGNATION from \"%@\";",lib];
     }
     else
     {
         sqlString = [NSString stringWithFormat:@"select DESIGNATION from \"%@\" order by cast(%@ as numeric) %@;",lib, [DT sharedDT].sortedBy, [DT sharedDT].sortAscDesc]; 
     }

     const char *sql = [sqlString cStringUsingEncoding:NSASCIIStringEncoding];
     sqlite3_stmt *selectstmt;

     if(sqlite3_prepare_v2(db,sql,-1,&selectstmt, NULL)==SQLITE_OK)
     {
            while(sqlite3_step(selectstmt)==SQLITE_ROW)
            {
                [[DT sharedDT].designationsInLibrary addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,0)]];
            }
            sqlite3_finalize(selectstmt);
         selectstmt=nil;
     }
 }

}   
sqlite3_close(db);
[localPool release];
}

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

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

发布评论

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

评论(2

星光不落少年眉 2024-07-31 09:59:34

看起来,您在每个循环周期打开数据库,但在函数退出之前仅关闭一次

所以尝试更改:

    }   
sqlite3_close(db);
[localPool release];
}

 
       sqlite3_close(db);
   }   

[localPool release];
}

或者甚至更好的更改:

for(int i=0;i [[DT sharedDT].typesInLibrary count];i++)
{   

 if(sqlite3_open([databasePath UTF8String], &db)==SQLITE_OK)
 {
     if (sqlite3_exec(db, "PRAGMA CACHE_SIZE=50;", NULL, NULL, NULL) != SQLITE_OK) {
         NSAssert1(0, @"Error: failed to set cache size with message '%s'.", sqlite3_errmsg(db));
     }

到:

if(sqlite3_open([databasePath UTF8String], &db)==SQLITE_OK)
{
    if (sqlite3_exec(db, "PRAGMA CACHE_SIZE=50;", NULL, NULL, NULL) != SQLITE_OK) {
         NSAssert1(0, @"Error: failed to set cache size with message '%s'.", sqlite3_errmsg(db));     
    }

    for(int i=0;i [[DT sharedDT].typesInLibrary count];i++)
    {   
    ...

因为您总是打开同一个数据库

It seems, that you're opening db on every loop cycle, but close only once, before function exit

So try to change:

    }   
sqlite3_close(db);
[localPool release];
}

to

 
       sqlite3_close(db);
   }   

[localPool release];
}

Or even better change:

for(int i=0;i [[DT sharedDT].typesInLibrary count];i++)
{   

 if(sqlite3_open([databasePath UTF8String], &db)==SQLITE_OK)
 {
     if (sqlite3_exec(db, "PRAGMA CACHE_SIZE=50;", NULL, NULL, NULL) != SQLITE_OK) {
         NSAssert1(0, @"Error: failed to set cache size with message '%s'.", sqlite3_errmsg(db));
     }

to:

if(sqlite3_open([databasePath UTF8String], &db)==SQLITE_OK)
{
    if (sqlite3_exec(db, "PRAGMA CACHE_SIZE=50;", NULL, NULL, NULL) != SQLITE_OK) {
         NSAssert1(0, @"Error: failed to set cache size with message '%s'.", sqlite3_errmsg(db));     
    }

    for(int i=0;i [[DT sharedDT].typesInLibrary count];i++)
    {   
    ...

because you're always open the same database

江心雾 2024-07-31 09:59:34

尝试使用以下命令调用 sqlite3_exec:

pragma cache_size=1 

Sqlite 似乎会占用内存进行缓存。

Try invoking sqlite3_exec with:

pragma cache_size=1 

Sqlite seems to gobble up memory for caching.

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