Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

what is the best way Clearing, releasing NSMutableArray? and when to do so?

FerrariXFerrariX Posts: 67Registered Users @
edited December 2009 in iOS SDK Development
Hi,

I have this array that I created using [[NSMutallableArray alloc] init],
What is the best way to clear and free up memory and when should I do it?


Method 1:
for(int i=0;i<[arrTiles count];i++)
{
    [arrTiles removeObjectAtIndex:i];
}
[arrTiles release];

Method 2:
arrTiles = nil;
[arrTiles release];

I'm thinking method 2 would be better b/c I don't have iterate through the entire array. Is setting an array to nil an expensive operation comparing to loop iteration?

Please tell me if there's a better way to release arrays.

Should I release it when I'm done with it in a method then re-allocate it when I need it again? or should I just let it hang around?

Does the rules as when to release depend on whether how the array was declared?
for example, either
[[NSMutableArray alloc] initWithCapacity:10]
or
[[NSMutallableArray alloc] init];
?

Thanks,

-FerrariX
Post edited by FerrariX on

Replies

  • smashersmasher Posts: 3,859Registered Users @ @ @ @ @
    edited December 2009
    All you should need is [myArray release] - everyone in the array will get released and, if the array is the only thing retaining them, dealloc'd.

    Post your code for creating items and adding them to the array, though. If you init, add to array, and then *don't* release right away then your items will have too many retains. This only applies if you use init, copy, or new -- not a convenience constructor -- so post your code.

    Method two will not work - you will leak the array. Setting to nil doesn't affect the retain count, and sending a "release" message to nil has no effect. Your method two is equivalent to this:
    arrTiles = nil;
    [nil release]; // this is a no-op; this line does nothing.
    

    And no, the rules on what happens to the objects inside the array don't change depending on how you created the array. The retain count of the array itself depends on how you created it, of course.

    Should you keep the array around or regen when needed? That's going to be very application specific. Do you need the array frequently, it's small in size, and expensive to create? Then keep it around. Do you need it infrequently, it takes a lot of memory, and it's cheap to recreate? Then recreate when needed.
    TinyCo is Hiring Mobile Game Programmers (C++)
    http://jobvite.com/m?3Ho5wgwr
  • FerrariXFerrariX Posts: 67Registered Users @
    edited December 2009
    Hi Smasher,

    Thanks for your advice.

    adding to array
    //add a character from arrLetters to a standard Tile image
    imgTile=[self addText:imgTile	text:[arrLetters objectAtIndex:IndexVal]];
    
    // init tileView to imgTile
    imgTileView=[[UIImageView alloc]initWithImage:imgTile];
    
    //add the tiles to array	 	
    [arrTiles insertObject:imgTileView atIndex:i];
    

    I make an array to hold UIImageView of ten tiles at a time, then whenever a tile is used, i update an indextCount. When indexCount equals to 10, I want to clear out the arrTiles and add new ones to it.
    if(indexCount==[arrTiles count])
    {
    	indexCount=0;				
    	for(int i=0;i<[arr count];i++)
           {
    	       [arrTiles removeObjectAtIndex:i];
    	}
    				
           [arrTiles release];
    	arrTiles=[[NSMutableArray alloc] initWithCapacity:10];
    }
    

    As for frequency, it happens about every 30 seconds.

    I don't know whether it would better to release the array and then init it again or just leave it alone.

    My fear is that somehow the array's contents not get released properly.

    I don't think it applies much to fixed size array such as unknown size array.

    Thanks again Smasher,

    -FerrariX
  • bravetargetbravetarget Posts: 519Registered Users
    edited December 2009
    you can also do

    [arrTiles removeAllObjects];
    <b><font color="DarkSlateGray">Water Your Body (ON SALE $0.99)</font></b><br />
    <a href="http://itunes.apple.com/us/app/water-your-body/id337998484?mt=8"; target="_blank">mzl.cigqxuxd.100x100-75
  • FerrariXFerrariX Posts: 67Registered Users @
    edited December 2009
    you can also do

    [arrTiles removeAllObjects];

    thanks bravetarget
  • Son of a BeachSon of a Beach Tasmania, AustraliaPosts: 225Registered Users @ @
    edited December 2009
    I don't see any point in removing any or all objects from an array just before releasing the array.

    Releasing the array will effectively remove and release all of its element objects anyway.
  • smashersmasher Posts: 3,859Registered Users @ @ @ @ @
    edited December 2009
    Right - because you used init, copy, or new to create the imgTileView, you need to release it. I'd release it right after you add it to the array.
    //add a character from arrLetters to a standard Tile image
    imgTile=[self addText:imgTile	text:[arrLetters objectAtIndex:IndexVal]];
    
    // init tileView to imgTile
    imgTileView=[[UIImageView alloc]initWithImage:imgTile];
    
    //add the tiles to array	 	
    [arrTiles insertObject:imgTileView atIndex:i];
    
    [imgTileView release];
    

    Now the only thing retaining the imgTileView is the array, so you only need to release the array to release everything in it - or call removeAllObjects if you're going to reuse the array.
    TinyCo is Hiring Mobile Game Programmers (C++)
    http://jobvite.com/m?3Ho5wgwr
  • FerrariXFerrariX Posts: 67Registered Users @
    edited December 2009
    Thank you smasher
  • LathikaLathika IndiaPosts: 1New Users Noob
    Hi EveryOne,

    It is better way to clear the NSMutableArray by using the following ways

    1. [arrTiles removeAllObjects];

    2. arrTiles=nil

    3. arrTile=[[NSMutableArray alloc]init];

  • Duncan CDuncan C Posts: 9,112Tutorial Authors, Registered Users @ @ @ @ @ @ @
    Lathika wrote: »
    Hi EveryOne,

    It is better way to clear the NSMutableArray by using the following ways

    1. [arrTiles removeAllObjects];

    2. arrTiles=nil

    3. arrTile=[[NSMutableArray alloc]init];

    This thread is VERY old, before ARC. now with ARC any of your 3 options would release all the images in the array (assuming there are no other strong references to them.)

    However, option 2 would leave arrTile as nil. Any future attempt to add an item to the array will send a message to nil, which will fail silently. Don't do that.

    Option 1 is probably ever-so-slightly more efficient than option 3, but unless you're doing it thousands of times in a loop you would not be able to detect the difference. Use either approach.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
Sign In or Register to comment.