Ilya Suzdalnitski
Ilya Suzdalnitski

Reputation: 53540

Memory leak involving NSString

I can't find a cause for a memory leak in my application. I found out that there is a memory leak through instruments and than more times I call the function than more memory leaks occurs. So it is obvious that a memory leak takes place.

Here is my code. Object:

@interface WordObject : NSObject
{
    int word_id;
    NSString *word;
}

@property int word_id;
@property (copy) NSString *word;

@end

method being used for populating UITableView:

-(void) reloadTableData {
        [tableDataArray removeAllObjects];

        for (int i = 0; i <= 25; i++)
        {
            NSMutableArray *words_in_section = [ [NSMutableArray alloc] init];
            [tableDataArray addObject:words_in_section];
            [words_in_section release];
        }

        WordObject *tempWordObj;

        int cur_section;

        while ( tempWordObj = [ [WordsDatabase sharedWordsDatabase] getNext] )
        {   
            cur_section = toupper([ [tempWordObj word] characterAtIndex:0 ] ) - 'A';

            NSMutableArray *temp_array = [tableDataArray objectAtIndex:cur_section];
            [temp_array addObject:tempWordObj];
            [tableDataArray replaceObjectAtIndex:cur_section withObject:temp_array];
        }

        [mainTableView reloadData];

        [mainTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
    }

getting content for cells:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
    }


    WordObject *tempWordObj = [ [tableDataArray objectAtIndex:[indexPath section] ] objectAtIndex:[indexPath row] ];

    if (!tempWordObj)
    {
        return cell;
    }

    cell.text = [tempWordObj word];

    return cell;
}

here is how I clean up memory:

-(void) freeMemory
{   
    if (tableDataArray)
    {
        [tableDataArray release];
        tableDataArray = nil;
    }
}

function that is being called from reloadTableData:

    -(WordObject*) getNext {
    if(sqlite3_step(getStmt) == SQLITE_DONE)
    {
        sqlite3_reset(getStmt);
        return nil;
    }


    WordObject *tempWord = [ [WordObject alloc] init];
    tempWord.word_id = sqlite3_column_int(getWordsStmt, 0);

    tempWord.word = [NSString stringWithUTF8String:(char *)sqlite3_column_text(getWordsStmt, 1)]; //Here a leak occurs

    return [tempWord autorelease];
}

And the leaked objects is [WordObject word].

I will be very grateful to anyone who will help me to solve this issue.

Upvotes: 1

Views: 1566

Answers (1)

Georg Sch&#246;lly
Georg Sch&#246;lly

Reputation: 126105

Add this method to WordObject:

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

This code makes sure, that the property word is released when a WordObject instance is deleted.


I'm pretty sure that this code belongs in a dealloc method too. BTW, you don't need tableDataArray = nil.

- (void)freeMemory
{   
    if (tableDataArray)
    {
        [tableDataArray release];
        tableDataArray = nil;
    }
}

Upvotes: 4

Related Questions