Reputation: 8765
In Objective-C, if I have a method in which I allocate and initialize an object, then return it, where/how do I release it?
for example, let's say I have a method where I create an object:
- (void)aMethod {
UIView *aView = [self createObject];
}
- (UIView *)createObject {
UIView *returnView = [[UIView alloc] initWithFrame:CGRectZero];
return returnView;
}
When do I release this object? Or would I just autorelease it?
Upvotes: 3
Views: 247
Reputation: 11003
- (void)aMethod {
UIView *aView = [self createObject];
}
- (UIView *)createObject {
UIView *returnView = [[UIView alloc] initWithFrame:CGRectZero];
[returnView autorelease];
return returnView;
}
Upvotes: 2
Reputation: 39
Remember also that Garbage Collection is not present on an iPhone so you can't autorelease if you are developing for that environment.
As to when you should release the object, the simple answer is when you are finished using it and before you destroy your application.
Upvotes: -6
Reputation: 20236
The rules for memory management are clear on this matter. You should read them. Very simple, and fundamental for writing Objective-C code using Apple's frameworks.
Upvotes: 8