Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 14, 2009, 09:08 PM   #1
Howiieque
macrumors regular
 
Join Date: Feb 2009
about memory managing--dealloc & autorelease

Code:
//  Controller.h

#import <Cocoa/Cocoa.h>


@interface Controller : NSObject {
    IBOutlet NSImageView *imageView;
    NSMutableArray * images;
    int currentImage;
}
- (IBAction)nextImage:(id)sender;

@end

//  Controller.m

#import "Controller.h"

@implementation Controller

- (void)awakeFromNib
{
    NSBundle * mainBundle = [NSBundle mainBundle];
    NSArray * imagePaths = [mainBundle pathsForResourcesOfType:@"jpg"
												   inDirectory:nil];
    images = [[NSMutableArray alloc] init];
    int count = [imagePaths count];
    int i;
    for (i = 0; i < count; i++) {
        NSImage * image = [[NSImage alloc] initWithContentsOfFile:
						   [imagePaths objectAtIndex:i]];
        [images addObject:image];
        [image release];
    }
    currentImage = 0;
    [imageView setImage:[images objectAtIndex:currentImage]];
}

- (IBAction)nextImage:(id)sender
{
    currentImage++;
    if (currentImage == [images count]) {                              // a
        currentImage = 0;
    }
    [imageView setImage:[images objectAtIndex:currentImage]];          // b
}

@end
this example is from learning cocoa with objective-c 2nd. it is an application shows the picture in the bundle and has a button to show the next image.

My question is :
1 there is a instance variable that owned by this class. It is allocated in the awakefromnib method. Should I override the dealloc method to release this instance variable?
2 If I did so, it seems that the application did not invoke the dealloc method at all. I wonder why.
3 if i add a [images autorelease] in the awakefromnib method for later release, an error occurred when i hit the next button. That means the images has been released. but it should be added to the autoreleasepool and then get released when application terminate. things work properly in command line utility. why didn't it work here?
Howiieque is offline   0 Reply With Quote
Old Feb 15, 2009, 01:27 AM   #2
eddietr
macrumors 6502a
 
Join Date: Oct 2006
Location: Virginia
Quote:
Originally Posted by Howiieque View Post
My question is :
1 there is a instance variable that owned by this class. It is allocated in the awakefromnib method. Should I override the dealloc method to release this instance variable?
Yes

Quote:
2 If I did so, it seems that the application did not invoke the dealloc method at all. I wonder why.
It may be because you are not releasing this controller properly. And so this controller is never dealloc'ed.

Quote:
3 if i add a [images autorelease] in the awakefromnib method for later release, an error occurred when i hit the next button. That means the images has been released. but it should be added to the autoreleasepool and then get released when application terminate. things work properly in command line utility. why didn't it work here?
Remember that AppKit will create an autorelease pool for each event cycle. So if you autorelease an object in that context, it will be released before the next event cycle.

With a command line tool, you generally create and release your own autorelease pools. The template and most examples of this involve just one autorelease pool which is released right before the application terminates. This is probably why you think all autoreleases happen when any application terminates, which is not the case (especially when using AppKit)

Last edited by eddietr; Feb 15, 2009 at 01:39 AM.
eddietr is offline   0 Reply With Quote
Old Feb 15, 2009, 03:50 AM   #3
Howiieque
Thread Starter
macrumors regular
 
Join Date: Feb 2009
Grate explanation. thank you.

but the answer to question 2. i am still not quit understand. Why it didn't release properly. how can i improve it. and other example provided by the book mentioned above seems that don't invoke the dealloc at all.

i also read:
Note that when an application terminates, objects may not be sent a dealloc message since the process’s memory is automatically cleared on exit—it is more efficient simply to allow the operating system to clean up resources than to invoke all the memory management methods.--NSObject Class Reference
i am puzzled
Howiieque is offline   0 Reply With Quote
Old Feb 15, 2009, 05:04 AM   #4
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
If you need to do cleanup on application shutdown, use NSApplication's -applicationWillTerminate: delegate method rather than relying on dealloc. As an optimization, Cocoa won't always deallocate objects at shutdown since it knows they're about to go away anyway.
Catfish_Man is offline   0 Reply With Quote
Old Feb 15, 2009, 08:04 AM   #5
Howiieque
Thread Starter
macrumors regular
 
Join Date: Feb 2009
thank you. and your logo is pretty.
Howiieque is offline   0 Reply With Quote
Old Feb 15, 2009, 08:36 AM   #6
pstoehr
macrumors member
 
Join Date: Aug 2003
Location: Scheßlitz, Oberfranken, Bavaria, Germany
Gc?

Hi,

have you turned garbagge collection on or is it still off?

Peter
pstoehr is offline   0 Reply With Quote
Old Feb 15, 2009, 09:55 AM   #7
eddietr
macrumors 6502a
 
Join Date: Oct 2006
Location: Virginia
Quote:
Originally Posted by Howiieque View Post
but the answer to question 2. i am still not quit understand. Why it didn't release properly. how can i improve it. and other example provided by the book mentioned above seems that don't invoke the dealloc at all.
I may have read too much into your question. I assumed you were done with your controller and wondering why the dealloc was not being called.

If you use your controller for the entire lifetime of your application then you may never get the dealloc call. Because when the application terminates, it doesn't go through and dealloc every object because that would be a waste of time. Once the application is terminated, the OS is going to reclaim all it's memory anyway.

But, on the other hand, if your controller is not supposed to last the whole lifecycle of the application, then you can release it when it is not needed. Then when the release count is 0, your controller will get the dealloc message which in turn will release other objects.

Hope that makes sense?
eddietr is offline   0 Reply With Quote
Old Feb 16, 2009, 06:22 PM   #8
Howiieque
Thread Starter
macrumors regular
 
Join Date: Feb 2009
thank you so much. it really makes sense.
Howiieque 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
Pocket Gallery - Managing & Protecting Your Photos Mapeapps iPhone and iPod touch Apps 0 Dec 10, 2012 04:33 AM
iPad: Deploying & Managing iPads in K-12 environment? tocheeba iPad 3 Nov 8, 2012 08:24 AM
Managing & SEO Joomla chameron Web Design and Development 0 Aug 31, 2012 03:11 AM
autorelease confusion chhoda iPhone/iPad Programming 1 Jun 27, 2012 12:29 PM
Autorelease pool acting up ? KnightWRX iPhone/iPad Programming 0 Jun 4, 2012 07:04 PM

Forum Jump

All times are GMT -5. The time now is 01:29 AM.

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

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