when an autoreleased object is actually released?

Posted by psebos on Stack Overflow See other posts from Stack Overflow or by psebos
Published on 2010-03-23T08:02:22Z Indexed on 2010/03/23 8:03 UTC
Read the original article Hit count: 321

Hi,

I am new in objective-c and I am trying to understand memory management to get it right.

After reading the excellent
Memory Management Programming Guide for Cocoa by apple my only concern is when actually an autoreleased object is released in an iphone/ipod application. My understanding is at the end of a run loop. But what defines a run loop in the application?

So I was wondering whether the following piece of code is right. Assume an object

@implementation Test

- (NSString *) functionA {
    NSString *stringA;
    stringA = [[NSString alloc] initWithString:@"Hello" autorelease]
    return stringA;
}

- (NSString *) functionB {
    NSString *stringB;
    stringB = [self functionA];
    return stringB;
}

- (NSString *) functionC {
    NSString *stringC;
    stringC = [self functionB];
    return stringC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    (NSString *) p = [self functionC];
    NSLog(@"string is %@",p);
}

@end

Is this code valid?

From the apple text I understand that the NSString returned from functionA is valid in the scope of functionB. I am not sure whether it is valid in functionC and in viewDidLoad.

Thanks!

© Stack Overflow or respective owner

Related posts about cocoa-touch

Related posts about objective-c