Hi!

I have been running my iPhone application through the Leaks Instrument in an 
attempt to further understand what I have been coding, however, I fear I do not 
know enough about how retain/release work to be able to fix my leaks, and I was 
wondering if I could paste one trouble spot function that I have identified, 
and if someone could be so kind as to point out how I can fix the leaks and how 
to code them out in the future?

My function returns a row from a SQLite database as an NSDictionary, and is as 
follows:

-(NSDictionary *)fetch {
        NSMutableDictionary *output = nil;
        if (db != nil && sth != nil) {
                int c = sqlite3_column_count(sth);
                if (fields == nil) {
                        fields = [[NSMutableArray alloc] init];
                        for (int i = 0; i < c; ++i) {
                                const char *name = sqlite3_column_name(sth, i);
                                [fields addObject:[NSString 
stringWithUTF8String:name]];
                        }
                }
                if (sqlite3_step(sth) == SQLITE_ROW) {
                        output = [[[NSMutableDictionary alloc] init] 
autorelease];
                        for (int i = 0; i < c; ++i) {
                                const unsigned char *val = 
sqlite3_column_text(sth, i);
                                NSString *value;
                                if (val == nil) {
                                        value = @"{NULL}";
                                }
                                else {
                                        value = [NSString 
stringWithUTF8String:(char *)val];
                                }
                                [output setObject:value forKey:[fields 
objectAtIndex:i]];
                        }
                }
        }
        return output;
}

The first leak reported by instruments is on the line where I alloc and init 
fields as an NSMutable array.  Firstly, fields is declared in my header, and is 
released in my dealloc method, so I am unsure what I have missed here and would 
be glad for any pointers on this problem.

The second is reported in the following line:

[fields addObject:[NSString stringWithUTF8String:name]];

Now, I can see that there is an anonymous string being created, but I am 
totally unsure as to how I can avoid leaking memory right here?

The third leak reported is here:

output = [[[NSMutableDictionary alloc] init] autorelease];

Now, I thought by using autorelease I was able to sidestep the issue of 
releasing this object (and I am pretty sure that adding this help fix an error 
that clang reported - could this be a false positive?)

And the last error is where I create the NSString value from the char *val - I 
believe this to be a similar class of error as my second leak, and would 
probably be addressed in a similar fashion.

So, sorry for such a lengthy email, and for asking so much of someone's time, 
but I hope that by seeing where I am going wrong here, I will be able to adopt 
good practices early on and make my code more robust in the future.

Many thanks to the poor soul who has to teach me the error of my ways in 
advance :o)

Mikey_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to