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-16-2011, 05:57 AM   #1 (permalink)
rws
Registered Member
 
Join Date: Jul 2011
Location: Birmingham, UK
Posts: 2
rws is on a distinguished road
Question NSArray initWithArray memory leak - help!

I'm scratching my head over a memory leak with an instance variable NSArray which I'm filling with the contents of a temporary (and then released) NSMutableArray. The contents are UIButtons, which I'm releasing when I'm finished with them in a separate method. The leaking code seems to be below according to Instruments, leaking 208 bytes each time 'createButtons' is called again (it creates a grid of UIButtons 8x8):

Code:
-(void)createButtons {
    for (int i=0; i<8; i++) {
        for (int j=0; j<8; j++) {
            
            UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
            but.frame = CGRectMake(i*60, j*60, 60, 60);
           
            [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [but setTitle:@"text" forState:UIControlStateNormal];
            [self.view addSubview:but];
            
            [but addTarget:self action:@selector(pressLetter:) forControlEvents:UIControlEventTouchUpInside];
            
            [tempTiles insertObject:but atIndex:(i*8)+j];
            
        }
    }
    
    // tiles is declared as an instance variable of class
    tiles = [[NSArray alloc] initWithArray:tempTiles]; // INSTRUMENTS POINTS TO THIS LINE FOR LEAK
    
    [tempTiles release];

}

// method called later to remove buttons from screen for next grid
-(void)removeButtons {
   if (tiles!=nil) {
        for (int i=0; i<[tiles count]; i++) {
            UIButton *but = [tiles objectAtIndex:i];
            [but removeFromSuperview];
            [but release];
        }
        tiles = nil;
    }
}
Hope someone can shed some light on this - thanks!
rws is offline   Reply With Quote
Old 07-16-2011, 07:12 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by rws View Post
I'm scratching my head over a memory leak with an instance variable NSArray which I'm filling with the contents of a temporary (and then released) NSMutableArray. The contents are UIButtons, which I'm releasing when I'm finished with them in a separate method. The leaking code seems to be below according to Instruments, leaking 208 bytes each time 'createButtons' is called again (it creates a grid of UIButtons 8x8):

Code:
-(void)createButtons {
    for (int i=0; i<8; i++) {
        for (int j=0; j<8; j++) {
            
            UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
            but.frame = CGRectMake(i*60, j*60, 60, 60);
           
            [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [but setTitle:@"text" forState:UIControlStateNormal];
            [self.view addSubview:but];
            
            [but addTarget:self action:@selector(pressLetter:) forControlEvents:UIControlEventTouchUpInside];
            
            [tempTiles insertObject:but atIndex:(i*8)+j];
            
        }
    }
    
    // tiles is declared as an instance variable of class
    tiles = [[NSArray alloc] initWithArray:tempTiles]; // INSTRUMENTS POINTS TO THIS LINE FOR LEAK
    
    [tempTiles release];

}

// method called later to remove buttons from screen for next grid
-(void)removeButtons {
   if (tiles!=nil) {
        for (int i=0; i<[tiles count]; i++) {
            UIButton *but = [tiles objectAtIndex:i];
            [but removeFromSuperview];
            [but release];
        }
        tiles = nil;
    }
}
Hope someone can shed some light on this - thanks!
It looks to me like you create the tiles array using initWithArray. That means you need to release tiles when you are done with it. In your removeButtons method, you set tiles to nil without releasing it. That looks like your leak right there. Just add a [tiles release] before setting tiles to nil in removeButtons, and it should solve your problem.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 07-16-2011, 07:13 AM   #3 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

However I advice you to read that
http://www.iphonedevsdk.com/forum/ip...roperties.html
__________________
dany_dev is offline   Reply With Quote
Old 07-18-2011, 06:13 AM   #4 (permalink)
rws
Registered Member
 
Join Date: Jul 2011
Location: Birmingham, UK
Posts: 2
rws is on a distinguished road
Smile

Quote:
Originally Posted by Duncan C View Post
It looks to me like you create the tiles array using initWithArray. That means you need to release tiles when you are done with it. In your removeButtons method, you set tiles to nil without releasing it. That looks like your leak right there. Just add a [tiles release] before setting tiles to nil in removeButtons, and it should solve your problem.
Thanks guys, really appreciated.

The leak is fixed now - adding the release as Duncan said did the trick. At first it was crashing, but I realised that I was also running a loop right before it to release all the button objects in the array before releasing it - not necessary as releasing the array sent a release to all those objects automatically anyway.

So the updated, unleaking code is:

Code:
-(void)removeButtons {
   if (tiles!=nil) {
        for (int i=0; i<[tiles count]; i++) {
            UIButton *but = [tiles objectAtIndex:i];
            [but removeFromSuperview];
            // [but release]; (not necessary)
        }
        [tiles release];
    }
}
rws is offline   Reply With Quote
Reply

Bookmarks

Tags
initwitharray, memory leak, nsarray, uibutton

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: 402
16 members and 386 guests
blasterbr, buggen, Clouds, dre, EvilElf, HemiMG, jeroenkeij, jimmyon122, LEARN2MAKE, Mah6447, n00b, nyoe, pungs, Sami Gh, stanny, toon4413
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,121
Posts: 402,900
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

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