views:

142

answers:

5

EDIT: I stuck in a single call to manually retain the array right after initializing it, and that fixed it. Dur. Thanks.

I keep an NSMutableArray in a UIScrollView subclass, and I add to it instances of a UIImageView subclass as each is added as a subview. It needs to be a mutable array, as I plan to rearrange their order in the array eventually; this bug is occurring before I even try to do so. I've used NSLogs to show that the array has count 10 up until and including applicationDidBecomeActive: is called in my AppDelegate, and added a timer to fire every 0.001s in the run loop. The first time it fires, it can't find the array anymore! I'm very confused and have been trying to track this down for hours.

The timer's working properly (i.e. when printCount: doesn't access the array, it's fine), it's only when I try to access the array there that it breaks. The regular call to printCount: below works, but the first time the timer fires I What could possibly be happening between these two calls to printCount:?

- (void)applicationDidBecomeActive:(UIApplication *)application {
    NSTimer *timer = [[NSTimer scheduledTimerWithTimeInterval:0.001f
                                                       target:[viewController view]
                                                     selector:@selector(printCount)
                                                     userInfo:nil
                                                      repeats:YES]
                      retain];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    [[viewController view] printCount];
}

I've been endlessly stepping through the program with the debugger. The array is correct right up through this point, and its memory address is the same even after its contents are changed. However, there is only object in it afterwards -- its 10 elements get replaced by a single UITouchData instance. No idea where it comes from. None of my event-handling methods attempt to reassign the array or change it. Any ideas what could be going wrong? I'm completely mystified.

+1  A: 

Did you -retain the array?

KennyTM
Yes, it's a retained property, synthesized in the implementation.
Dan R.
A: 

Perhaps your object was released? What happens if you retain it. Does that work or you just get a memory leak?

PeanutPower
A: 

its 10 elements get replaced by a single UITouchData instance. No idea where it comes from.

Is it always a UITouchData instance? Always? -- Cause that would seem like a dead give-away (if its not the SAME every-time) of a memory problem.

Mr-sk
A: 

I stuck in a call to retain to array right after initializing it, and that fixed it. Dur.

Thank you for the suggestions. As usually happens with problems like these, it was very simple and finally giving in and asking for help triggers a solution and makes me feel stupid. :D

Dan R.
A: 

Does the view initialize and release the array?
Are you sure that the NSView isn't released in between?
You could check if the memory address of [viewController view] (== self within printCount) is the same every time your timer calls printCount.

weichsel