-1

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?

Kazuki Sakamoto
  • 13,929
  • 2
  • 34
  • 96
Andrew Lauer Barinov
  • 5,694
  • 10
  • 59
  • 83
  • Your example code is invalid: it uses FooViewController and BazViewController both as type names and variable names. This makes it difficult to tell what's going on: Is the BazViewController being assigned to a local variable? What is responsible for retaining it? – tc. Jun 11 '12 at 17:58
  • Code style corrected, I was writing pseudocode though – Andrew Lauer Barinov Jun 11 '12 at 18:00
  • In my attempted solution I created a strong mutable array property within the parent view controller and added all the child view controllers in, hoping that this strongly preserved reference would cause ARC to not deallocate their memory prematurely. – Andrew Lauer Barinov Jun 11 '12 at 18:04
  • Pseudocode is rarely helpful for figuring out where a missing retain is. What is retaining the parent VC? – tc. Jun 11 '12 at 18:09
  • VC hierarchy is UINavigationController > GridViewController > BazViewController > FooViewController; GridViewController is a UIStoryboard scene and it has a strong array property holding all the BazViewControllers, and each BazViewController has an analogous strong array property of FooViewControllers – Andrew Lauer Barinov Jun 11 '12 at 18:13

2 Answers2

3

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];
Andrew Lauer Barinov
  • 5,694
  • 10
  • 59
  • 83
1

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.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • What if there's a case of 1,2 or 3 `FooViewControllers` inside of the parent view controller? Create properties for all 3 and use them as necessary? – Andrew Lauer Barinov Jun 11 '12 at 17:18