views:

24

answers:

1

I have a UIScrollView with an Image subView. I subclass touchesEnded to add a dialog box as a subView as follows:

- (void) pushInfoBox {
    // the following line is referred to "myVC Caller" below
    myViewController *myVC = [[myViewController alloc] 
                              initWithNibName:@"myView" bundle:nil];
    [self addSubview:myVC.view];
    [myVC release];
}

The view shows correctly, but when I click myVC.view.backButton the code execution jumps to the "myVC Caller" line above. The next step dumps with -[myViewController performSelector:withObject:withObject:]: message sent to deallocated instance.

Strangely the myCaller.IBAction (backButton action) is not executed.

I expect the user interaction enabled between the scrollview and the pushed message box interfere with each other, but am unsure how to handle this correctly.

any ideas on how to tackle this?

+1  A: 

There are several major issues with your code. I'll walk through it:

- (bool)pushInfoBox:(int)xPos down:(int)yPos window:(int)wPos {

First of all, you're not returning a BOOL value in this method so the return type should be void (if it was returning a BOOL, note that its uppercase, not lowercase as you have it now). Second, you are supplying xPos, yPos, and wPosas arguments but they are not being used in the method. So your method declaration should look more like this:

- (void)pushInfoPox;

The next problem is here:

myViewController *myVC = [[myViewController alloc] initWithNibName:@"myView" bundle:nil];

You are allocating an instance of myViewController (by the way, class names should always have their first letter capitalized) but never releasing it, so you are causing a memory leak. You should be declaring myVC as an instance variable and a retained property in your header:

@interface ...
{
    ...
    myViewController *myVC
}
...
@property (nonatomic, retain) myViewController myVC;
@end

You should be allocating it like this now:

myVC = [[myViewController alloc] initWithNibName:@"myView" bundle:nil];

And it should be released in the dealloc method:

- (void)dealloc
{
    [myVC release];
    [super dealloc];
}

The last problem is in this line:

[mBox release];

I don't see any object called mBox being retained in that method, so that line should not be there.

macatomy
Hi Macatomy, thanks for your detailed answer. I aplologize for the lousy question - just tried simplifying the routine. I added another Edit to correct.to the points you make:(a) BOOL - I took out the logic I use since it's not related to the problem. Changed bool to uppercase.(b) Parameters xPos, yPos and wPos - same here, took it out of the sample for simplification(c) releasing myVC - that's actually the "[MBox release]" call later. => question updated
iFloh
There's still a problem. You are allocating the view controller, adding its view as a subview, and then immediately releasing it. This is what's causing your problem. Like I said in my answer, myVC should be a retained instance variable, and it should be released in dealloc, not inside the method.
macatomy
You're right, following your advise it works, but I don't understand the difference ?
iFloh
What's happening is, when you allocate a view controller, add the view, and then immediately release the view controller, it crashes because the view is owned by the view controller (which doesn't exist anymore because it has been deallocated). Basically, for as long as you want to use that view, its corresponding view controller must not be deallocated.
macatomy
thanks a lot :)
iFloh