Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 10, 2009, 11:28 PM   #1
Darkroom
Guest
 
Join Date: Dec 2006
Location: Montréal, Canada
Dealloc With Fast Enumeration?

in my awakeFromNib method i have added most of my NIB objects into mutable arrays so i can call them in my code. is it ok to release those objects using fast enumeration in the dealloc method?

Code:
- (void)dealloc
{
for (id objects in array1)
	[objects release];

[array1 release];
		
for (id objects in array2)
	[objects release];

[array2 release];
[super dealloc];
}
Darkroom is offline   0 Reply With Quote
Old Jul 10, 2009, 11:56 PM   #2
kpua
macrumors 6502
 
Join Date: Jul 2006
Why would you need to? Why not let the array just hold onto the last reference, so that by releasing the array, you release all the objects?
kpua is offline   0 Reply With Quote
Old Jul 11, 2009, 01:02 AM   #3
larkost
macrumors 6502a
 
Join Date: Oct 2007
You can't change the object you are iterating over in fast enumeration.
larkost is offline   0 Reply With Quote
Old Jul 11, 2009, 01:09 AM   #4
Darkroom
Thread Starter
Guest
 
Join Date: Dec 2006
Location: Montréal, Canada
i'm setting properties for all of the objects, including the array objects. so even if i add those objects to an array in the init method, don't i still have to release each object, and not just the array that holds them?

but anyway, i've realized that since they are mutable arrays, the contents of the arrays with be changing through out the program, so i'll just have to release each one individually in dealloc to make sure they all get released.
Darkroom is offline   0 Reply With Quote
Old Jul 11, 2009, 02:59 AM   #5
kpua
macrumors 6502
 
Join Date: Jul 2006
I see. Honestly, I'm curious why you're putting them in an array in the first place. It seems redundant. Why not just have them as ivars if you've already got @properties for them?

Furthermore, it is indeed OK to -release objects in an array in fast enumeration. You can even modify their contents. The only restriction is that you don't modify the array structure itself—no addObject:, removeObject: calls that modify the pointers that the array contains or the size of the array.

(NOTE: The same does not hold true of sets or keys in dictionaries. Once those objects get put into the collection, you should not modify them at all, because you might change the hash value, causing the collection to lose track of the objects.)
kpua is offline   0 Reply With Quote
Old Jul 11, 2009, 03:07 AM   #6
Darkroom
Thread Starter
Guest
 
Join Date: Dec 2006
Location: Montréal, Canada
thanks.

the reason i'm throwing objects into arrays is because i'm sometimes animating most of them with the same movements, except for one or two, which i remove from the array before animation. it's code refactoring. instead of listing 20 variables within several animation methods, removing an object or two before calling animations on one NSMutableArray using fast enumeration is easier to maintain.
Darkroom is offline   0 Reply With Quote
Old Jul 11, 2009, 05:36 PM   #7
autorelease
macrumors regular
 
Join Date: Oct 2008
Location: Achewood, CA
Quote:
Originally Posted by Darkroom View Post
thanks.

the reason i'm throwing objects into arrays is because i'm sometimes animating most of them with the same movements, except for one or two, which i remove from the array before animation. it's code refactoring. instead of listing 20 variables within several animation methods, removing an object or two before calling animations on one NSMutableArray using fast enumeration is easier to maintain.
Shouldn't you be using inheritance, so each class knows how to perform its movements? Removing items from an array before performing an operation on the entire array is inefficient and kinda defeats the purpose of using an object-oriented language...

Also, collection classes retain objects added to them and release them on dealloc. If the objects you put into an array are autoreleased after creation (or created with factory methods) then you don't have to worry about releasing the individual members of the array.
__________________
*** -[NSAutoreleasePool autorelease]: Cannot autorelease an autorelease pool
autorelease is offline   0 Reply With Quote
Old Jul 11, 2009, 08:56 PM   #8
Darkroom
Thread Starter
Guest
 
Join Date: Dec 2006
Location: Montréal, Canada
Quote:
Originally Posted by autorelease View Post
Shouldn't you be using inheritance, so each class knows how to perform its movements? Removing items from an array before performing an operation on the entire array is inefficient and kinda defeats the purpose of using an object-oriented language...
not sure i follow you. i'm adding objects like UIButtons, UIImageViews and Labels to the array. if a certain button is clicked (or tapped since it's on iPhone), that button is removed from the array and enlarged, while all the other objects within the array are animated off screen. but if a different button was tapped, the first button would remain in the array so it too would be animated off screen. which ever button is tapped, it will execute a different animation than all the other objects in the array. all these buttons, views and labels are members of an elite squad known as the special victims unit *record scratch* sorry... they're all members of a view controller class, so i'm not sure what you mean by suggesting the use of inheritance in animating select objects.
Darkroom is offline   0 Reply With Quote
Old Jul 12, 2009, 01:26 AM   #9
autorelease
macrumors regular
 
Join Date: Oct 2008
Location: Achewood, CA
Quote:
Originally Posted by Darkroom View Post
not sure i follow you. i'm adding objects like UIButtons, UIImageViews and Labels to the array. if a certain button is clicked (or tapped since it's on iPhone), that button is removed from the array and enlarged, while all the other objects within the array are animated off screen. but if a different button was tapped, the first button would remain in the array so it too would be animated off screen. which ever button is tapped, it will execute a different animation than all the other objects in the array. all these buttons, views and labels are members of an elite squad known as the special victims unit *record scratch* sorry... they're all members of a view controller class, so i'm not sure what you mean by suggesting the use of inheritance in animating select objects.
Okay, I see what you mean. I couldn't really tell what you were trying to accomplish from your previous post.

You should probably keep one array for all objects, and create another one just for the objects you want to animate. Put the objects you want to animate into this second array before you start the animation, and empty it when the animation is finished.
__________________
*** -[NSAutoreleasePool autorelease]: Cannot autorelease an autorelease pool
autorelease is offline   0 Reply With Quote
Old Jul 12, 2009, 01:45 AM   #10
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by autorelease View Post
Okay, I see what you mean. I couldn't really tell what you were trying to accomplish from your previous post.

You should probably keep one array for all objects, and create another one just for the objects you want to animate. Put the objects you want to animate into this second array before you start the animation, and empty it when the animation is finished.
Or keep one array, period:
Code:
theOne = clickedThing;
for(id myThing in myControls) {
  if(myThing == theOne) continue;
  [myThing animate];
}
-Lee
lee1210 is offline   0 Reply With Quote
Old Jul 13, 2009, 08:34 AM   #11
jared_kipe
macrumors 68030
 
jared_kipe's Avatar
 
Join Date: Dec 2003
Location: Seattle
Send a message via AIM to jared_kipe
Since you are simply sending a message to every object, why not just us NSArray's instance method:
Code:
- (void)makeObjectsPerformSelector:(SEL)aSelector
Meaning something like..
Code:
 [myArray makeObjectsPerformSelector: @selector(release) ];
I would think this is as fast or faster.
jared_kipe is offline   0 Reply With Quote
Old Jul 13, 2009, 02:24 PM   #12
Littleodie914
macrumors 68000
 
Littleodie914's Avatar
 
Join Date: Jun 2004
Location: Rochester, NY
Maybe I misunderstood your question, but why not just release the objects once they are placed into the array in the beginning? Eg:

Code:
- (void)awakeFromNib {
    NSMutableArray arrayOfThings = [[NSMutableArray alloc] init];
    [arrayOfThings addObject:tableView];
    [tableView release];
    [arrayOfThings addObject:textField];
    [textField release];
}
When you call 'release' on an array, it also sends release to all of its contained elements. So if the retain count of any of the objects in your array is '1', they will be automatically released with the array. Eg:

Code:
- (void)dealloc {
    [arrayOfThings release]; // Release is also sent to all elements
    [super release];
}
Littleodie914 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iPhone 5C charging fast - Too fast? iamvogue iPhone 8 Mar 16, 2014 10:22 PM
iPhone 5 charging fast – too fast? iLoveMyApple iPhone 19 Dec 31, 2012 11:48 PM
Lightning cable charges iPhone fast, real fast Myiphone7 iPhone 1 Oct 2, 2012 04:03 AM
why is my dealloc not called? Tex-Twil iPhone/iPad Programming 2 Jul 19, 2012 03:35 PM

Forum Jump

All times are GMT -5. The time now is 05:23 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC