So, you are saying I should call a retain when I get my reference so that it is 
kept as an instance var?

On 27 Mar 2010, at 19:33, Sandor Szatmari wrote:

> Every time this method runs you would loose the reference to the memory 
> previously allocated for the fields array.  This happens when you assign a 
> newly allocated array to feilds.
> 
> You should either release fields before  reassigning it (conditionally if it 
> is not nil), or do not reallocate memory for it, instead empty it and reuse 
> the existing memory.
> 
> Sandor Szatmari
> 
> On Mar 27, 2010, at 15:06, Michael Davey <frak....@gmail.com> wrote:
> 
>> 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/admin.szatmari.net%40gmail.com
>> 
>> This email sent to admin.szatmari....@gmail.com

_______________________________________________

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