开发者

trouble fixing memory leak iphone

开发者 https://www.devze.com 2023-03-23 02:21 出处:网络
i have 3 memory leaks in my application and i don\'t find how to fix it. I\'m kind of new to xcode and objective c. 开发者_开发知识库Here is the code i have:

i have 3 memory leaks in my application and i don't find how to fix it. I'm kind of new to xcode and objective c. 开发者_开发知识库Here is the code i have:

if(sqlite3_prepare_v2( [[DatabaseController sharedDatabaseController] getDb], sqlQueryConverted, -1, &dbStatement, NULL)==SQLITE_OK){

        //Run the query
        while ( sqlite3_step(dbStatement) == SQLITE_ROW ) 
        {    


            const char *name = (const char *)sqlite3_column_text(dbStatement, 0);
            int courseId = sqlite3_column_int(dbStatement, 1);
            const char *location = (const char *)sqlite3_column_text(dbStatement, 2);
            const char *date = (const char *)sqlite3_column_text(dbStatement, 3);

            //Convert the returnedElement char to string
            nameConverted = [[NSString alloc] initWithUTF8String:name];
            locationConverted = [[NSString alloc] initWithUTF8String:location];
            dateConverted = [[NSString alloc] initWithUTF8String:date];

            Course *course = [[[Course alloc]initWithName:nameConverted _id:courseId location:locationConverted courseDate:dateConverted] autorelease];

            //Add the course to the to a temporary list to remove duplicated items            
            [tempResults addObject:course];

        }  
        [nameConverted release];
        [locationConverted release];
        [dateConverted release];
    }

I have tried to autorelease it too. This code is being used to filter a search and reload a search display table. If i put the release line in the while statement the application would crash if i type 2 letters. How could i fix this?

Thanks.

EDIT: I've been going back and forth with this problem with no luck. I've come to the conclusion that there's something wrong with Instruments because it's still showing memory leaks. Here's the code as it is today and as i believe should fix the problem:

NSString *nameConverted = [[NSString alloc] initWithUTF8String:name];
            NSString *locationConverted = [[NSString alloc] initWithUTF8String:location];
            NSString *dateConverted = [[NSString alloc] initWithUTF8String:date];

            Course *course = [[[Course alloc]initWithName:nameConverted _id:courseId location:locationConverted courseDate:dateConverted] autorelease];

            //Add the course to the to a temporary list to remove duplicated items            
            [tempResults addObject:course];
            course = nil;
            [course release];
            [nameConverted release];
            nameConverted = nil;
            [locationConverted release];
            locationConverted = nil;
            [dateConverted release];
            dateConverted = nil;
            NSLog(@"course retain count %i",[course retainCount]);
            NSLog(@"name coverted retain count %i",[nameConverted retainCount]);
            NSLog(@"location coverted retain count %i",[locationConverted retainCount]);
            NSLog(@"date coverted retain count %i",[dateConverted retainCount]);

The logs are telling me that the retainCount = 0; so i don't understand why there's a memory leak. Can you guys give me some advice?

Thanks again.


You're leaking at each loop. You are only releasing the 3 lasts NSString. Everytime you re-assign a new NSString to your 3 variables (nameConverted, locationConverted, dateConverted) you loose the reference to the NSString objects they are pointing too. That means memory leaking. You only release the 3 last ones of them, when you get out of your While loop.


You are creating a memory block each while loop, and then only releasing it once. So if your while loop runs over 10 times, each string has a retain count of 10 but only released once.

To fix, put your 3 releases inside your while loop like this:

if(sqlite3_prepare_v2( [[DatabaseController sharedDatabaseController] getDb], sqlQueryConverted, -1, &dbStatement, NULL)==SQLITE_OK){

        //Run the query
        while ( sqlite3_step(dbStatement) == SQLITE_ROW ) 
        {    


            const char *name = (const char *)sqlite3_column_text(dbStatement, 0);
            int courseId = sqlite3_column_int(dbStatement, 1);
            const char *location = (const char *)sqlite3_column_text(dbStatement, 2);
            const char *date = (const char *)sqlite3_column_text(dbStatement, 3);

            //Convert the returnedElement char to string
            nameConverted = [[NSString alloc] initWithUTF8String:name];
            locationConverted = [[NSString alloc] initWithUTF8String:location];
            dateConverted = [[NSString alloc] initWithUTF8String:date];

            Course *course = [[[Course alloc]initWithName:nameConverted _id:courseId location:locationConverted courseDate:dateConverted] autorelease];

            //Add the course to the to a temporary list to remove duplicated items            
            [tempResults addObject:course];

        [nameConverted release];
        [locationConverted release];
        [dateConverted release];

        }  

    }


Have you tried to use:

nameConverted = [[NSString initWithUTF8String:name] autorelease];
locationConverted = [[NSString initWithUTF8String:location] autorelease];
dateConverted = [[NSString initWithUTF8String:date] autorelease];

And removing the releases outside the loop? Also do not autoremove the course object, release it after adding it to tempResults.


This is how you should be doing it.

NSString *nameConverted = [[NSString alloc] initWithUTF8String:name];
NSString *locationConverted = [[NSString alloc] initWithUTF8String:location];
NSString *dateConverted = [[NSString alloc] initWithUTF8String:date];

Course *course = [[Course alloc] initWithName:nameConverted _id:courseId location:locationConverted courseDate:dateConverted];
[tempResults addObject:course];
[course release];

[dateConverted release];
[locationConverted release];
[nameConverted release];
0

精彩评论

暂无评论...
验证码 换一张
取 消