Re: ARC Release too soon
Re: ARC Release too soon
- Subject: Re: ARC Release too soon
- From: Martin Hewitson <email@hidden>
- Date: Sun, 17 Mar 2013 07:09:11 +0100
If it's for windows where the number of them is under user control, then I typically add them to a an array (which is a strongly retained property) then you can either listen for window closing notifications and remove them, or if the user may open the same window again, then you can check the array before creating a new controller. If the controller for that window exists, then you can just reshow it. I use this pattern a lot, so it was easy for me to find an example:
- (void) showEditorForItem:(id)item
{
// check if we have an editor array already
if (self.editorControllers == nil) {
self.editorControllers = [NSMutableArray array];
}
BOOL foundController = NO;
for (EditorController *editor in self.editorControllers) {
if (editor.item == item) {
[editor showWindow:self];
foundController = YES;
break;
}
}
if (foundController == NO) {
EditorController *editor = [[EditorController alloc] initWithItem:item managedObjectContext:self.managedObjectContext];
[editor showWindow:self];
[self.editorControllers addObject:editor];
}
}
- Martin
On 16, Mar, 2013, at 10:38 PM, Chris Paveglio <email@hidden> wrote:
> Sure I totally understand that. My question is more of "what is the most elegant way to do it". Add them to an array?
>
>
> ----- Original Message -----
> From: iain <email@hidden>
> To: Chris Paveglio <email@hidden>
> Cc: Cocoa Dev List <email@hidden>
> Sent: Saturday, March 16, 2013 2:35 PM
> Subject: Re: ARC Release too soon
>
>
>
> On 16 Mar 2013, at 06:16 PM, Chris Paveglio <email@hidden> wrote:
>
>> So, am I doing some fundamental window management wrong (not sure since old app worked OK and didn't seem to leak), or how do I do something so ARC doesn't dealloc window controllers at the end of the function that fires them off
>
> If you want the window controllers to hang around after the function finished you need to assign them to a strong variable otherwise they will be released.
>
> Iain
> _______________________________________________
>
> 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
_______________________________________________
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