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 02-22-2011, 07:08 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 59
kend0g187 is on a distinguished road
Default Memory management question (involving arrays)

What's the best way to handle the following situation: You have an NSMutableArray property in your controller class. In viewDidLoad(), you load it up with newly-allocated objects. Then later on, you swap out an object for a new one. Something like:

Code:
- (void) viewDidLoad {
MyObject *object = [[MyObject alloc] init];
[myArray addObject: object];

- (void) someOtherFunction {
MyObject *newObject = [[MyObject alloc] init];
[myArray replaceObjectAtIndex: 0 withObject: newObject];
}

- (void) dealloc {
[myArray release];
}
The way I got it to work was to use autoreleases anytime I declared a new object, like MyObject *object = [[[MyObject alloc] init] autorelease];, but Instruments says I have a leak.

Also, I tried doing it the following way, and I get EXC_BAD_ACCESS, but I don't understand why:

Code:
- (void) viewDidLoad {
MyObject *object = [[MyObject alloc] init];
[myArray addObject: object];

- (void) someOtherFunction {
MyObject *tmp = [myArray objectAtIndex: 0];
MyObject *newObject = [[MyObject alloc] init];
[myArray replaceObjectAtIndex: 0 withObject: newObject];
[tmp release];
}

- (void) dealloc {
for( MyObject* object in myArray ) {
[object release];
}
[myArray release];
}
So basically I have three questions: which way is best? why does the first way (with autoreleases) show leaks? and why does the second way not work?
kend0g187 is offline   Reply With Quote
Old 02-22-2011, 07:14 PM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Arrays retain objects added to them and release removed ones. So the first batch looks ok if you were to add autorelease calls to both alloc/inits, or if you called release on object and newObject after you add them to the array.
baja_yu is offline   Reply With Quote
Old 02-22-2011, 07:25 PM   #3 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 34
VinceYuan is on a distinguished road
Default

In a managed memory environment, an object receives a retain message when it’s added; in a garbage collected environment, it is strongly referenced. When an array is deallocated in a managed memory environment, each element is sent a release message.

In a managed memory environment, when an object is removed from a mutable array it receives a release message. If that object is owned only by that array, the object is deallocated when it is removed. If you want to use the object after its removal, you should therefore typically send it a retain message before it’s removed from the array.

Code:
- (void) viewDidLoad {
MyObject *object = [[MyObject alloc] init];
[myArray addObject: object]; //myArray retains object
[object release];
}

- (void) someOtherFunction {
MyObject *newObject = [[MyObject alloc] init];
[myArray replaceObjectAtIndex: 0 withObject: newObject];
[newObject release];
}

- (void) dealloc {
[myArray release];
}
VinceYuan is offline   Reply With Quote
Old 02-22-2011, 07:51 PM   #4 (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 kend0g187 View Post
What's the best way to handle the following situation: You have an NSMutableArray property in your controller class. In viewDidLoad(), you load it up with newly-allocated objects. Then later on, you swap out an object for a new one. Something like:

Code:
- (void) viewDidLoad {
MyObject *object = [[MyObject alloc] init];
[myArray addObject: object];

- (void) someOtherFunction {
MyObject *newObject = [[MyObject alloc] init];
[myArray replaceObjectAtIndex: 0 withObject: newObject];
}

- (void) dealloc {
[myArray release];
}
The way I got it to work was to use autoreleases anytime I declared a new object, like MyObject *object = [[[MyObject alloc] init] autorelease];, but Instruments says I have a leak.

Also, I tried doing it the following way, and I get EXC_BAD_ACCESS, but I don't understand why:

Code:
- (void) viewDidLoad {
MyObject *object = [[MyObject alloc] init];
[myArray addObject: object];

- (void) someOtherFunction {
MyObject *tmp = [myArray objectAtIndex: 0];
MyObject *newObject = [[MyObject alloc] init];
[myArray replaceObjectAtIndex: 0 withObject: newObject];
[tmp release];
}

- (void) dealloc {
for( MyObject* object in myArray ) {
[object release];
}
[myArray release];
}
So basically I have three questions: which way is best? why does the first way (with autoreleases) show leaks? and why does the second way not work?

Your first block of code is really close. You just need to fix it like this:

Code:
- (void) viewDidLoad {
MyObject *object = [[[MyObject alloc] init] autorelease];
[myArray addObject: object];

- (void) someOtherFunction {
MyObject *newObject = [[[MyObject alloc] init] autorelease];
[myArray replaceObjectAtIndex: 0 withObject: newObject];
}

- (void) dealloc {
[myArray release];
}
You just need the autoreleases I added.

As Baya said, arrays retain objects that are added to them, and release objects when they are removed. So if you add an autoreleased object to an array, it sticks around as long as it is in the array, and gets released as soon as you remove it from the array.

Your first block of code was creating objects with alloc/init. Objects created with alloc/init have a retain count of 1, and it is your job to release them when you are done with them. You then add them to your array, which causes them to have a retain count of 2. When you remove them from the array, their retain count drops to 1, so they don't get deallocated.

I'm not sure why your second try is crashing, but what you are doing is a band-aid to fix the original 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 02-23-2011, 02:37 PM   #5 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 59
kend0g187 is on a distinguished road
Default

Well, I actually got both ways working, so it must have been something else that was causing the EXC_BAD_ACCESS. But I have another question. Say the MyObject class has an object as one of it's properties, something like:

Code:
@interface MyObject : NSObject {
	UIImageView *imageView;
}

@implementation MyObject
@synthesize imageView;
- (void) dealloc {
	[imageView release];
	[super dealloc];
}
It appears as though, when you send a release to a MyObject, the release IS NOT sent to imageView, so the following code causes a leak:

Code:
- (void)viewDidLoad {
MyObject *object = [[MyObject alloc] init];
object.imageView = [[UIImageView alloc] init];
[myArray addObject: object];
}
- (void)dealloc {
	for( MyObject *object in myArray ) {
		[object release];
	}
[myArray release];
}
After the object is loaded into the array, object and object.imageView both have a retain count of 2. When dealloc is called, the for loop decrements object to 1, but object.imageView is still at 2. Then when the array releases, object goes to 0 and the MyObject dealloc method gets called taking object.imageView down to 1, not 0. What's the solution?
kend0g187 is offline   Reply With Quote
Old 02-23-2011, 02:45 PM   #6 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

1. Define a retainer property for your imageView in the header file. @property (nonatomic, retain) UIImageView *imageView;
2. In viewDidLoad you need to release (or autorelease) the new MyObject and UIImageView or you will be leaking them both.
3. Again, you do not need to loop and release objects in an array. When you send release to an array it will automatically send release to each of its objects.

4. Read this, it will make things much more clear: http://www.iphonedevsdk.com/forum/ip...roperties.html

Last edited by baja_yu; 02-23-2011 at 02:48 PM.
baja_yu is offline   Reply With Quote
Reply

Bookmarks

Tags
array, autorelease, management, memory, nsmutablearray

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: 379
18 members and 361 guests
Absentia, akphyo, apatsufas, BinHex, cpsclicker, dre, Error404, Gaz, gmarro, jeroenkeij, Kirkout, mottdog, Music Man, PavelMik, teebee74, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

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