开发者

iPhone memory leak problem

When I 'build and analyze' I don't have any warnings.No warnings at all. But when I run 'performance tool' I have some red blocks there :( I have a table, every cell has star (favorite button) and when I tap star performance tool display red block there. This is my method. I have tried to 'release' indexPath and channel but then my application crash :( I am still learning memory management so please help?

- (void) didTapStar: (UIButton *) button withEvent: (UIEvent *) event
{   
    NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: [[[event touchesForView: button] anyObject] locationInView: self.tableView]];
    int selectedItemRowIndex = indexPath.row;

    Channel *channel = [tableData objectAtIndex:selectedItemRowIndex];  
    channel.IsFavorite = !channel.IsFavorite;

    if(channel.IsFavorite == YES)
    {       
        [button setImage:[UIImage imageNamed:IMG_FAVORITE_STARRED] forState:UIControlStateNormal];      
    }
    else 
    {
        [button setImage:[UIImage imageNamed:IMG_FAVORITE_STAR] forState:UIControlStateNormal];
    }

    [self setSelectedChannelFavoriteStatus:channel];        

    // Reloading data

    [itemsData removeAllObjects];
    [searchedData removeAllObjects];
    [tableData removeAllObjects];

    [self initializeDatabaseStructure];     
    [self getChannelsForSelectedItem];

    Channel *co;    
    for(co in itemsData)
    {
        img1 = [UIImage imageNamed:co.IconPath];
        if(img1 == nil)
        {           
            continue;
        }
        else
        {
            co.IconImage = img1;                                        
        }
    }

    [tableData addObjectsFromArray:itemsData];

    [self.tableView reloadData];        
}

- (void) initializeDatabaseStructure
{
    if(sqlite3_open([[self filePath] UTF8String], &db_beta) != SQLITE_OK)
    {           
        sqlite3_close(db_beta);
        NSAssert(0, MSG_DATABASE_FAILED_TO_OPEN);
    }   
}


- (void) setSelectedChannelFavoriteStatus:(Channel *)channel
{
    NSString *tempIsFavorite;

    if(channel.IsFavorite == YES)
    {
        tempIsFavorite = [[NSString alloc]initWithString:@"1"];
    }
    else 
    {
        tempIsFavorite = [[NSString alloc]initWithString:@"0"];
    }

    NSMutableString *sql = [NSMutableString stringWithFormat:@"UPDATE tblChannels SET IsFavorite = '%@' WHERE ChannelId = %i", tempIsFavorite, channel.ChannelId];

    char *err;
    if(sqlite3_exec(db_beta, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK)
    {
        sqlite3_close(db_beta);
        NSAssert(0, MSG_ERROR_UPDATING_TABLE);
    }
    else 
    {
        NSLog(@"Updating favorite field.");
    }   

    [tempIsFavorite release];
}


- (void) getAllChannels
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];  
    NSString *myString = [prefs stringForKey:PREF_PROVIDER];

    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM tblChannels WHERE ChannelProvider = '%@' ORDER BY ChannelName", myString];

    sqlite3_stmt *cmd;  

    if(sqlite3_prepare_v2(db_beta, [sql UTF8String], -1, &cmd, nil) == SQLITE_OK)
    {
        while(sqlite3_step(cmd) == SQLITE_ROW)
        {               
            char *clnChannelId = (char *) sqlite3_column_text(cmd, 0);
            NSString *m_ChannelId = [[NSString alloc] initWithUTF8String:clnChannelId];

            char *clnChannelName = (char *) sqlite3_column_text(cmd, 1);
            NSString *m_ChannelName = [[NSString alloc] initWithUTF8String:clnChannelName];

            char *clnChannelLogo = (char *) sqlite3_column_text(cmd, 2);
            NSString *m_ChannelLogo = [[NSString alloc] initWithUTF8String:clnChannelLogo];

            char *clnChannelUrl = (char *) sqlite3_column_text(cmd, 3);
            NSString *m_ChannelUrl = [[NSString alloc] initWithUTF8String:clnChannelUrl];   

            char *clnChannelIsFavorite = (char *) sqlite3_column_text(cmd, 5);
            NSString *m_ChannelIsFavorite = [[NSString alloc] initWithUTF8String:clnChannelIsFavorite]; 

            Channel *c = [[Channel alloc]init];
            c.ChannelId = [m_ChannelId integerValue];
            c.Name = m_ChannelName;
            c.IconPath = m_ChannelLogo;
            c.StreamUrl = m_ChannelUrl; 

            if ([m_ChannelIsFavorite isEqualToString:@"0"]) 
            {
                c.IsFavorite = NO;
            }
            else 
            {
                c.IsFavorite = YES;
     开发者_StackOverflow中文版       }

            [itemsData addObject:c];

            [m_ChannelId release];
            [m_ChannelName release];
            [m_ChannelLogo release];
            [m_ChannelUrl release];
            [m_ChannelIsFavorite release];
            [c release];
        }           
        sqlite3_finalize(cmd);                      
    }
}


You have a lot of class methods called, they may have leaks!

[self setSelectedChannelFavoriteStatus:channel];        
[self initializeDatabaseStructure];     
[self getChannelsForSelectedItem];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜