5

I've run into this problem a couple of times and want to know the correct approach to take.

For an example, let's say I'm writing an iPhone app and I want a custom alert view class that uses blocks.

So I write the class, then later on in my code I go:

MyAlertView *alert = [MyAlertView alertWithBlahBlahBlah...];
[alert addButton:@"button" withBlock:^{ ... }];
[alert show];

Somewhere in the alert view class, we have

- (void)addButton:(NSString *)button withBlock:(void (^))block {
    [_blocks setObject:[block copy] forKey:button];
}

- (void)show {
    ... drawing stuff ...
    UIButton *button = ...
    [button addTarget:self selector:@selector(buttonPressed:) ...];
    ...
}

- (void)buttonPressed:(id)sender {
    ((void (^)())[_blocks objectForKey:[sender title]])();
}

So, the alert view now shows up just fine. The problem is, if I tap a button, it attempts to send the buttonPressed: selector to the MyAlertView object that was displayed. The MyAlertView has, however, been removed from the superview at this time. ARC decides that because the alert view is not owned by anyone anymore, it should be deallocated, not knowing that a button needs to message it in the future. This causes a crash when the button is tapped.

What's the right way to keep the alert view in memory? I could make the MyAlertView object a property of the class that's using it, but that's kind of silly (what if I want to show two alerts at once?).

7
  • 1
    Why has it been removed from its superview if it's still on screen?
    – jscs
    Jul 25, 2012 at 3:07
  • 3
    If you don't have a reference to it somewhere, then you wouldn't be able to deallocate it when you want to "at some point in the future", therefore you have to maintain some kind of reference to it. This could be in a property, as you suggest, or perhaps an array of "cached" objects which you will be able to dispose of when your class is deallocated.
    – lnafziger
    Jul 25, 2012 at 3:10
  • @JoshCaswell This is mostly just a contrived example. I was doing something similar though where the action would happen after the view had been removed from the screen. Jul 25, 2012 at 3:22
  • 1
    Delegate methods are only sent AS it's dismissed. After alertView:didDismissWithButtonIndex (which is probably sent in some sort of target/action method of UIAlertView), nothing related to that particular UIAlertView instance is being sent anymore. Your alert view should do the same thing: add the button to itself, not to its superview (which would be considered bad practice anyway). Jul 25, 2012 at 3:28
  • 2
    Right, as @Scott says, when the UIAlertView button is pressed, it sends the delegate messages before it is removed from its superview, which then deallocates it unless you have a reference to it stored away somewhere.
    – lnafziger
    Jul 25, 2012 at 3:32

3 Answers 3

7

If an object were to remain in memory, and you do not have a reference to it, this is known as a memory leak. As I said in my comments, you need to keep some kind of reference to it so that a) it is not deallocated, b) you can send a message to it, and c) you can deallocate it before your class is deallocated.

The most obvious way to do this would be with a property in your class. Since you said that you don't want to do that (maybe you have a lot of them) then another possible solution would be to keep an array of cached objects that you plan on reusing and eventually deallocating.

1
  • I'll go ahead and mark this as the right answer because clearly this isn't a problem with ARC, it's a problem with me managing my references. I'll have to go try and implement that caching idea, that seems like the cleanest approach. Jul 25, 2012 at 4:27
1

I think you can use performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay to retain the alert view in runloop.

Actually, I just came across an wrapper implementation for UIAlertView using this skill.

Check UIAlertView input wrapper for more detail.

1
  • This actually does solve my problem pretty nicely. The only quibble I have with it is it feels too hacky to put in production code; if for some reason my user left their phone out with the alert view open for a few hours, weird stuff might happen. I guess if the timeout just spawned another timer and kept going forever, this might be a good solution? Jul 25, 2012 at 4:26
1

Quite simply, you're breaking the memory management rules. ARC doesn't change the rules, it just automates them. If you need an object to stay alive, it needs to have an owner. Every object in your app's object graph, all the way back to the application delegate, has an owner. It may not be obvious what that owner is (and sometimes the owner may be an autorelease pool), but there is one.

If you want this view to stick around, it needs to be owned by something, even if it's not "currently being used". If it's onscreen, it should be part of the view hierarchy. If it's not, the ideal owner is likely to be the object that created it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.