Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-25-2011, 11:39 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 22
verbatimbot is on a distinguished road
Default Help with code snippet - memory warning with level 1

Hi guys,
I would like to ask for you opinion on the following code. There are two methods (the first one makes a request for data, and the second one process them). In the app itself it looks like this - on load, app gets the first set of xml data (20 items) that displays in uitableview. At the end 'load more' button appears which brings another set etc.. I have tested cellForRowAtIndexPath method first (by returning an empty cell) just to see if the memory warning will be produced and it does, meaning the cause is probably in one of these two methods.

So i get "Received memory warning. Level=1" after i load 22 pages (thats 22 x 20 = 440 xml items that I hold in my NSMutableArray)

Code:
-(void) loadApps:(NSString *)passedMode withPage:(NSInteger)passedPage showActivityIndicator:(BOOL)showIndicator
{
    // showIndicator is YES only for the first time, when the app initially loads the first set of data
    if (showIndicator)
        [self showActivityIndicator];
    
    useActivityIndicator = showIndicator;
    
    page = passedPage;
    [mode setString:passedMode];
    
    // Create the request.
    NSMutableString *apiUrl = [[NSMutableString alloc] initWithFormat:@"%@?mode=newest", [AppSettings getServiceUrl]];
    [apiUrl appendFormat:@"&page=%d",passedPage];
    [apiUrl appendFormat:@"&view=%@",mode];
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:apiUrl]
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:60.0];
    

    [UrlRequestManager addHeaders:theRequest];
    
    
    [apiUrl release];
    // create the connection with the request
    // and start loading the data
    serviceCallInProgress = YES;
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    
    if (theConnection) {
        // Create the NSMutableData to hold the received data.
        // receivedData is an instance variable declared elsewhere.
        NSMutableData *md = [[NSMutableData alloc] init];
        [self setReceivedData:md];
        [md release];
    }
    
    [theConnection release];
}
Code:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Received response, connectionDidFinishLoading begun...");
    serviceCallInProgress = NO;
    
    CXMLDocument *doc = [[CXMLDocument alloc] initWithData:[self receivedData] options:0 error:nil];
    
    
    
    NSArray *nodes = [doc nodesForXPath:@"//ItunesRecordObject" error:nil];
    
    int counter = 0;
    for(CXMLNode *node in nodes)
    {
        counter++;
        
        
        NSString *str = [[NSString alloc] initWithString:[[node nodeForXPath:@"Stars" error:nil] stringValue]];
        
        NSMutableDictionary *d = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                  [[node nodeForXPath:@"Id" error:nil] stringValue], @"id", 
                                  [[node nodeForXPath:@"TrackName" error:nil] stringValue], @"name", 
                                  [[node nodeForXPath:@"ArtistName" error:nil] stringValue], @"artist", 
                                  [[node nodeForXPath:@"Genre" error:nil] stringValue], @"genre", 
                                  [[node nodeForXPath:@"Price" error:nil] stringValue], @"price", 
                                  [[node nodeForXPath:@"ThumbnailImage" error:nil] stringValue], @"imageurl", 
                                  [[node nodeForXPath:@"PrettyDate" error:nil] stringValue], @"releasedate", 
                                  [[node nodeForXPath:@"Permalink" error:nil] stringValue], @"permalink", 
                                  [[node nodeForXPath:@"RatesCount" error:nil] stringValue], @"ratescount", 
                                  [[node nodeForXPath:@"CommentsCount" error:nil] stringValue], @"commentscount",
                                  [[node nodeForXPath:@"IsFeatured" error:nil] stringValue], @"featured",
                                  str, @"rate",
                                  nil, @"image", 
                                  nil];
        
        [[self dataSourceArray] addObject:d];
        [d release];
        
        // add to existing or create a new section record
        bool exists = NO;
        for (int i=0; i<[sections count]; i++)
        {
            NSMutableDictionary *dict = [[self sections] objectAtIndex:i];           
            
            
            if ([[dict objectForKey:@"releasedate"] isEqualToString:[[node nodeForXPath:@"Group" error:nil] stringValue]])
            {
                
                
                exists = YES;
                int count = [[dict objectForKey:@"count"] intValue];
                count++;
                
                [dict setValue:[NSString stringWithFormat:@"%d", count] forKey:@"count"];
                
                
                break;
            }
        }
        
        if (!exists)
        {
            // add new one
            NSString *relDate = [[NSString alloc] initWithString:[[node nodeForXPath:@"Group" error:nil] stringValue]];
            
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:relDate, @"releasedate", @"1", @"count", [NSString stringWithFormat:@"%d", [sections count]], @"section", nil];
            [[self sections] addObject:dict];
            [dict release];
            [relDate release];
        }
        
        
        [str release];
    }
    
    
    if ([[doc nodeForXPath:@"//TotalPages" error:nil] stringValue] != nil)
    {
        NSString *totalPagesStr = [[NSString alloc] initWithString:[[doc nodeForXPath:@"//TotalPages" error:nil] stringValue]];
        totalPages = [totalPagesStr intValue];
        [totalPagesStr release];
    }
    
    [[self tableViewList] setDelegate:self];
    [[self tableViewList] setDataSource:self];
    
    [[self tableViewList] reloadData];
    tableViewList.hidden = NO;
    
    
    
    if (page == 1)
    {
        [self hideActivityIndicator];
        [[self tableViewList] scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    }
    
    
    //[receivedData release];
    [doc release];
    
    // release the connection, and the data object
    //[connection release];
    NSLog(@"connectionDidFinishLoading completed.");
}
Any help would be very much appreciated.
verbatimbot is offline   Reply With Quote
Old 07-25-2011, 03:58 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

Have you tried running the build & analyze tool to see if it will identify any potential memory leaks?
smithdale87 is offline   Reply With Quote
Old 07-26-2011, 04:04 AM   #3 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 22
verbatimbot is on a distinguished road
Default

Just reported a few 'object sent -autorelease too many times', like this one in the IF selection. Any idea what can this be?

Code:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == [comments count])
    {
        UITableViewCell *c = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if (c == nil) {
            c = [[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease] initWithFrame:CGRectMake(5, 5, 60, 60)];
        }  
        
        if ([self.comments count] > 0) // *** object sent -autorelease too many times
        {
...
verbatimbot is offline   Reply With Quote
Reply

Bookmarks

Tags
memory management, memory warning

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 401
16 members and 385 guests
7twenty7, blasterbr, buggen, chiataytuday, Clouds, dre, fiftysixty, HemiMG, jimmyon122, jonathandeknudt, LEARN2MAKE, n00b, nyoe, pungs, tymex, UMAD
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,668
Threads: 94,121
Posts: 402,902
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jonathandeknudt
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:59 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0