views:

452

answers:

2

I have a UITableView that when scrolled to far bounces bag. This also means the user can drag the table so that only half of it is visible and the other half of the main view is white. This causes some weird behavior in my app. I create an array in the AppDelegate. In RootViewController's viewDidLoad, I get a reference to the AppDelegate and keep it around until the app shuts down. In RootViewController's cellForRowAtIndexPath, I reference the AppDelegate instance's array to get objects that will fill cell labels. That works fine until the user scrolls as mentioned above. Once that happens, in can see in the debugger that object 7, for instance, in the array has turned into UICachedDeviceRGBColor. I have no idea why that happens. The array is only assigned once in AppDelegate on app creation. It is only read referenced from then on.

From what I can tell, I must first click a row in the table. The new view is pushed, which also has a UITableView. in the cellForRowAtIndexPath of this 2nd view, I check the delegate.array, since 2nd view also has a reference to the array. I immediately see that object 2 in the array is:

2 : UIDeviceRGBColorSpace 1 1 1 1

which it shouldn't be. Then when I click back and the main table displays again, do the above scroll technique to get the weirdness. I scroll the table just enough to hit my breakpoint in cellForRowAtIndexPath and see the array has been altered. Object two is the correct type but now object 7 isn't.

The problem is that if I try to access object 7 and call a property on the type I'm casting to, that property of course doesn't exist on UICachedDeviceRGBColor. It's an "Inavlid" and so I get the “EXC_BAD_INSTRUCTION" because of bad memory access.

What causes this alter of the array under this specific condition? Or at the least, how can I guard against it?

A: 

Is this on the simulator or the device?

I found something like this would happen only on the simulator, when I used the scroll wheel on my mouse to scroll the table. The simulator can't tell when you release the scroll wheel, so doesn't know when to pop back. Then when I touch it will pop back and register my touch on a different item than I touched. This didn't happen on the device. When you scroll on the device with the scroll bar, it pops back as soon as you release, so you can't touch an item when over-scrolled.

GoatRider
+3  A: 

This is on the device but looks as though I've figured it out.

In didSelectRowAtIndexPath of RootViewController, I get one of the objects out of delegate.thearray. Just before 2nd View is pushed, I release this object. That's wrong since I didn't alloc the object. Removed that line and no more weirdness in the array. Things seem to be functioning as normal now.

4thSpace
This is not an answer. Please comment on GoatRider's answer.
Can Berk Güder
That is the answer. He released the memory when he shouldn't have and another object was allocated in its place. This is an absolutely classic early-release bug. 4thspace, good on you for solving it.
Brent Royal-Gordon
Thanks. Not sure why the OP got a -1 vote.
4thSpace