Instruments 说这个方法存在内存泄漏,但我不知道在哪里

发布于 2024-11-17 09:38:49 字数 1663 浏览 0 评论 0原文

根据仪器,

    cat.catName = (catName)?[NSString stringWithUTF8String:catName]:@"";
    NSData *dataForCachedImage = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 2) length: sqlite3_column_bytes(statement, 2)];           
    cat.catThumb = [UIImage imageWithData:dataForCachedImage];
    [dataForCachedImage release];

以下代码中的这些行存在内存泄漏。

我还静态分析了代码..显示该文件没有问题。

-(NSMutableArray *)getAllItems{
NSMutableArray *items = [[[NSMutableArray alloc] init] autorelease];

const char *sql = "SELECT * FROM category ORDER by sort";

sqlite3_stmt *statement;

int sqlresult = sqlite3_prepare(database, sql, -1, &statement, nil);

if (sqlresult == SQLITE_OK) {
    while (sqlite3_step(statement) == SQLITE_ROW) {
        Category *cat = [[Category alloc] init];

        char *catName = (char *)sqlite3_column_text(statement, 1);

        cat.catID = sqlite3_column_int(statement, 0);
        cat.catName = (catName)?[NSString stringWithUTF8String:catName]:@"";
        NSData *dataForCachedImage = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 2) length: sqlite3_column_bytes(statement, 2)];           
        cat.catThumb = [UIImage imageWithData:dataForCachedImage];
        [dataForCachedImage release];
        cat.catLock = sqlite3_column_int(statement, 3);
        cat.sort = sqlite3_column_int(statement, 4);
        cat.total = [self totalSMS:cat.catID];

        [items addObject:cat];

        [cat release];
    }
    sqlite3_finalize(statement);
}

else
{
    NSLog(@"problem with the database");
    NSLog(@"%d",sqlresult);
}
return items;}

有人可以指出吗? 谢谢

According to instrument there is memory leak at these lines

    cat.catName = (catName)?[NSString stringWithUTF8String:catName]:@"";
    NSData *dataForCachedImage = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 2) length: sqlite3_column_bytes(statement, 2)];           
    cat.catThumb = [UIImage imageWithData:dataForCachedImage];
    [dataForCachedImage release];

in the following code.

I have also statically analyzed the code.. shows no issue in this file.

-(NSMutableArray *)getAllItems{
NSMutableArray *items = [[[NSMutableArray alloc] init] autorelease];

const char *sql = "SELECT * FROM category ORDER by sort";

sqlite3_stmt *statement;

int sqlresult = sqlite3_prepare(database, sql, -1, &statement, nil);

if (sqlresult == SQLITE_OK) {
    while (sqlite3_step(statement) == SQLITE_ROW) {
        Category *cat = [[Category alloc] init];

        char *catName = (char *)sqlite3_column_text(statement, 1);

        cat.catID = sqlite3_column_int(statement, 0);
        cat.catName = (catName)?[NSString stringWithUTF8String:catName]:@"";
        NSData *dataForCachedImage = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 2) length: sqlite3_column_bytes(statement, 2)];           
        cat.catThumb = [UIImage imageWithData:dataForCachedImage];
        [dataForCachedImage release];
        cat.catLock = sqlite3_column_int(statement, 3);
        cat.sort = sqlite3_column_int(statement, 4);
        cat.total = [self totalSMS:cat.catID];

        [items addObject:cat];

        [cat release];
    }
    sqlite3_finalize(statement);
}

else
{
    NSLog(@"problem with the database");
    NSLog(@"%d",sqlresult);
}
return items;}

can someone point out?
Thanks

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

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

发布评论

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

评论(2

与酒说心事 2024-11-24 09:38:49

在第一个代码示例中,您没有释放 dataForCachedImage。您可以将其更改为

cat.catName = (catName)?[NSString stringWithUTF8String:catName]:@"";
NSData *dataForCachedImage = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 2) length: sqlite3_column_bytes(statement, 2)];           
cat.catThumb = [UIImage imageWithData:dataForCachedImage];
[dataForCachedImage release];

cat.catName = (catName)?[NSString stringWithUTF8String:catName]:@"";
NSData *dataForCachedImage = [[[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 2) length: sqlite3_column_bytes(statement, 2)] autorelease];           
cat.catThumb = [UIImage imageWithData:dataForCachedImage];

In the first sample of code you aren't releasing dataForCachedImage. You can either change it to

cat.catName = (catName)?[NSString stringWithUTF8String:catName]:@"";
NSData *dataForCachedImage = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 2) length: sqlite3_column_bytes(statement, 2)];           
cat.catThumb = [UIImage imageWithData:dataForCachedImage];
[dataForCachedImage release];

or

cat.catName = (catName)?[NSString stringWithUTF8String:catName]:@"";
NSData *dataForCachedImage = [[[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 2) length: sqlite3_column_bytes(statement, 2)] autorelease];           
cat.catThumb = [UIImage imageWithData:dataForCachedImage];
心碎无痕… 2024-11-24 09:38:49

好的..解决了!...问题是catThumb 没有在Category 类中发布。就是这样!,不需要其他修改......无论如何,谢谢大家!

ok.. resolved it!... the problem was that the catThumb was not released in Category class. thats it!, no other modification required... anyways, thx everybody!

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