Coriolan
Coriolan

Reputation: 71

Cannot figure out iPhone/Objective-C Memory Leak in sqlite data retrieval

I am trying to do some basic table retrieval from sqlite and I keep stumbling over some memory leak I cannot figure out, especially since it appears only with one of two equivalent lines.

What I do is simple :

-Create an object (category) to hold my sqlite DB line
-Have a method that queries sqlite and returns an array of these objetcs

I put all the code here (stripped it down a bit to simplify but the I have the memory leak with this very code).

category.h:

@interface Category : NSObject {
    int         catId;
    NSString    *description;
    int         order;
    NSString    *iconName;
}

@property(nonatomic, readwrite, assign) int catId;
@property(nonatomic, retain) NSString *description;
@property(nonatomic, readwrite, assign) int order;
@property(nonatomic, retain) NSString *iconName;


@end

category.m:

#import "Category.h"


@implementation Category

@synthesize catId;
@synthesize description;
@synthesize order;
@synthesize iconName;

@end

Method to retrieve array:

-(NSMutableArray *)getAll {

    NSString *query = "select id, description, catorder, image from category order by catorder";
    sqlite3_stmt    *statement;
    NSMutableArray  *categoryArray = [[NSMutableArray alloc] init];

    if (sqlite3_prepare_v2([[Database sharedDatabase] instance], [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            int     dbId =              sqlite3_column_int(statement, 0);
            char    *dbDescription =    (char *)sqlite3_column_text(statement, 1);
            int     dbOrder =           sqlite3_column_int(statement, 2);
            char    *dbIcon =           (char *)sqlite3_column_text(statement, 3);

            NSString *desc = [[NSString alloc] initWithUTF8String:dbDescription];
            NSString *icon = [[NSString alloc] initWithUTF8String:dbIcon];          

            Category *category = [[Category alloc] init];

            category.catId=dbId;
            category.description = desc;
            //category.description = @"Test";
            category.order = dbOrder;
            category.iconName = icon;
            //category.icon = @"test.png";

            [desc release];         
            [icon release];

            [categoryArray addObject:category];

            [category release];
        }
        sqlite3_finalize(statement);
    }
    [categoryArray autorelease];
    return categoryArray;
}

This line leaks

NSString *icon = [[NSString alloc] initWithUTF8String:dbIcon];          

This line doesn't

NSString *desc = [[NSString alloc] initWithUTF8String:dbDescription];

Here what instruments shows : enter image description here

and when I drill down : enter image description here

Upvotes: 0

Views: 315

Answers (1)

Vinnie
Vinnie

Reputation: 1740

You don't have a dealloc method. You need to have this in your Category.m file:

-(void)dealloc
{
    [description release];
    [iconName release];
    [super dealloc];
}

Also, I'm pretty sure you can't have a property named "description". That's a reserved method name.

Upvotes: 2

Related Questions