Re: Where or when do I release this object from this action.
Re: Where or when do I release this object from this action.
- Subject: Re: Where or when do I release this object from this action.
- From: Sherm Pendley <email@hidden>
- Date: Fri, 27 Feb 2009 18:25:42 -0500
On Fri, Feb 27, 2009 at 5:42 PM, James Cicenia <email@hidden> wrote:
> OK -
>
> The question is where do I release that object. If I put the release at the
> end of the method, nothing will pop into my view.
> If I don't release it, it works fine, but then I am worried about a leak.
>
> - (void)monthFruitAction:(id)sender{
>
> MonthPickerViewController *mpvc = [[MonthPickerViewController
> alloc]initWithNibName:@"MonthPicker" bundle:nil]; <<==== If I
> autorelase, this method will NOT show my view.
> [mpvc setMyParentController:self];
> mpvc.view.frame =CGRectMake(19,66,260,258);
> [fruitTypeView addSubview:mpvc.view];
> [mpvc release] <<==== If I put this in, this method will NOT show my view.
> }
>
> So, I am wondering how I am to release this object.
>
The question is, will you be done with it within the scope of this method?
As a general rule, if you want to keep something around for longer than
that, you need to make it an instance variable instead of a local variable.
That way you'll be able to release it later.
Since you're using dot-syntax, I assume you're using Objective-C 2.0. So,
declare a retained property, like this:
@property (retain) MonthPickerViewController *mpvc;
And, of course, synthesize the accessors for it:
@synthesize mpvc;
Now, since you've specified retain behavior for the accessors, you'll need
to autorelease the controller when you create it - otherwise you'd
over-retain it. Since it's no longer a local variable, you'll need to use
self.mpvc to access it:
- (void)monthFruitAction:(id)sender {
self.mpvc = [[[MonthPickerViewController alloc]
initWithNibName:@"MonthPicker"
bundle:nil] autorelease];
[self.mpvc setMyParentController:self];
self.mpvc.view.frame = CGRectMake(19,66,260,258);
[fruitTypeView addSubview:self.mpvc.view];
}
Now, when all is said and done, and you're finished with mpvc, just assign
nil to it. With retain symantics for the synthesized accessor methods, doing
so should release the old value:
self.mpvc = nil;
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden