Question

I am drawing a grid views and experiencing an EXC_BAD_ACCESS error when UIButton fires off a selector. Turning on zombie objects gives me the message:

*** -[FooViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x91818f0

Here is what I am doing:

BazViewController *baz = [[BazViewController alloc] initDesignatedInitializer];
FooViewController *foo = [[FooViewController alloc] initDesignatedInitializer];
[baz.view addSubview:foo.view];

I am running this on ARC, so I could see the possibility of FooViewController being prematurely deallocated.

So I did this hack:

Create an NSMutableArray ivar&property in BazViewController named viewControllers and did this instead:

BazViewController *baz = [[BazViewController alloc] initDesignatedInitializer];
FooViewController *foo = [[FooViewController alloc] initDesignatedInitializer];
[baz.view addSubview:foo.view];
[baz.viewControllers addObject:foo];

But I still get the same error above.

I proceeded to do the equivalent of the above on the view controller that deals with the grid. (Create a view controllers array, and add the object into it at time of that object's creation). Still no dice. Can anyone recommend a another way to prevent this vc from being prematurely released?

Was it helpful?

Solution

I resolved this issue myself.

Instead of creating arrays I used addChildViewController to add a strong reference to the appropriate vc's. No premature deallocation and no zombie objects.

BazViewController *baz = [[BazViewController alloc] initDesignatedInitializer];
FooViewController *foo = [[FooViewController alloc] initDesignatedInitializer];
[baz.view addSubview:foo.view];
[baz addChildViewController:foo];

OTHER TIPS

Create a FooViewController property in the class that's creating it and give the property a strong attribute. Assign to that rather than a local variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top