iPhone - update the property of nth object of a NSMutablearray from touchesMovedwithEvent

0

In my earlier post in iPhone - update the property of nth object of a NSMutablearray i got something that worked in viewDidLoad but when i am trying to use almost the same code in -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event then the application crashes.

@interface

int activeItem;
int startFrom;
NSMutableArray *iconsMutableArray;
NSMutableArray *textsMutableArray;

and then

@property int activeItem;
@property int startFrom;
- (NSMutableArray *)iconsMutableArray;
- (NSMutableArray *)textsMutableArray;


@synthesize activeItem;
@synthesize startFrom;

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    
    activeItem = (int)startFrom + (int)round(animationEndTo/AngleForEachItem);  
    [[iconsMutableArray objectAtIndex:activeItem] setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Guide-Icon-%d_Active.png", activeItem]]];
}

But my application fails to update the UIIMageView and crashes without showing any error message. Can anyone please help me? Thanks in advance.

Below is the crash report:

Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Codes: KERN_INVALID_ADDRESS at 0x0000000032200002 Crashed Thread: 0 Dispatch queue: com.apple.main-thread

Application Specific Information: objc_msgSend() selector name: objectAtIndex: iPhone Simulator 238.2, iPhone OS 4.3 (iPad/8F192) iPhone Simulator 238.2, iPhone OS 4.3 (iPad/8F192)

Thread 0 Crashed: Dispatch queue: com.apple.main-thread 0 libobjc.A.dylib 0x0197a09f objc_msgSend + 19 1 ??? 0x00229810 OBJC_CLASS_$_NSNumber + 0 2 UIKit 0x003b90f4 forwardMethod2 + 92 3 UIKit 0x003b90f4 forwardMethod2 + 92 4 UIKit 0x0030fd97 -[UIWindow _sendTouchesForEvent:] + 481 5 UIKit 0x002f0c37 -[UIApplication sendEvent:] + 447 6 UIKit 0x002f5f2e _UIApplicationHandleEvent + 7576 7 GraphicsServices 0x01e87992 PurpleEventCallback + 1550 8 CoreFoundation 0x017f5944 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 52 9 CoreFoundation 0x01755cf7 __CFRunLoopDoSource1 + 215 10 CoreFoundation 0x01752f83 __CFRunLoopRun + 979 11 CoreFoundation 0x01752840 CFRunLoopRunSpecific + 208 12 CoreFoundation 0x01752761 CFRunLoopRunInMode + 97 13 GraphicsServices 0x01e861c4 GSEventRunModal + 217 14 GraphicsServices 0x01e86289 GSEventRun + 115 15 UIKit 0x002f9c93 UIApplicationMain + 1160 16 MJA 0x00002bde main + 96 (main.m:14) 17 MJA 0x00002b75 start + 53

Thread 1: 0 libSystem.B.dylib 0x9119d922 kevent + 10 1 libdispatch_sim.dylib 0x01d92f36 _dispatch_mgr_invoke + 237 2 libdispatch_sim.dylib 0x01d93333 _dispatch_queue_invoke + 162 3 libdispatch_sim.dylib 0x01d93593 _dispatch_worker_thread2 + 261 4 libSystem.B.dylib 0x9119cd21 _pthread_wqthread + 390 5 libSystem.B.dylib 0x9119cb66 start_wqthread + 30

Thread 2: WebThread 0 libSystem.B.dylib 0x9117709a mach_msg_trap + 10 1 libSystem.B.dylib 0x91177807 mach_msg + 68 2 CoreFoundation 0x017f5d86 __CFRunLoopServiceMachPort + 150 3 CoreFoundation 0x01752e74 __CFRunLoopRun + 708 4 CoreFoundation 0x01752840 CFRunLoopRunSpecific + 208 5 CoreFoundation 0x01752761 CFRunLoopRunInMode + 97 6 WebCore 0x02f13913 RunWebThread(void*) + 499 7 libSystem.B.dylib 0x911a47fd _pthread_start + 345 8 libSystem.B.dylib 0x911a4682 thread_start + 34

Thread 3: 0 libSystem.B.dylib 0x9117709a mach_msg_trap + 10 1 libSystem.B.dylib 0x91177807 mach_msg + 68 2 CoreFoundation 0x017f5d86 CFRunLoopServiceMachPort + 150 3 CoreFoundation 0x01752e74 __CFRunLoopRun + 708 4 CoreFoundation 0x01752840 CFRunLoopRunSpecific + 208 5 CoreFoundation 0x01752761 CFRunLoopRunInMode + 97 6 Foundation 0x0008b102 +[NSURLConnection(NSURLConnectionReallyInternal) _resourceLoadLoop:] + 398 7 Foundation 0x00055cf4 -[NSThread main] + 81 8 Foundation 0x00055c80 __NSThread__main + 1387 9 libSystem.B.dylib 0x911a47fd _pthread_start + 345 10 libSystem.B.dylib 0x911a4682 thread_start + 34

Thread 4: com.apple.CFSocket.private 0 libSystem.B.dylib 0x91196066 select$DARWIN_EXTSN + 10 1 CoreFoundation 0x017872bc __CFSocketManager + 812 2 libSystem.B.dylib 0x911a47fd _pthread_start + 345 3 libSystem.B.dylib 0x911a4682 thread_start + 34

Thread 0 crashed with X86 Thread State (32-bit): eax: 0x05e0e840 ebx: 0x05a0ef11 ecx: 0x05a05484 edx: 0x05e6b200 edi: 0x32200002 esi: 0x00229810 ebp: 0xbfffd728 esp: 0xbfffd6c4 ss: 0x0000001f efl: 0x00010202 eip: 0x0197a09f cs: 0x00000017 ds: 0x0000001f es: 0x0000001f fs: 0x00000000 gs: 0x00000037 cr2: 0x32200002

iphone
objective-c
asked on Stack Overflow May 16, 2011 by itsazzad • edited May 23, 2017 by Community

3 Answers

2

Notice this at the top of your crash log;

Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Codes: KERN_INVALID_ADDRESS at 0x0000000032200002 Crashed Thread: 0 Dispatch queue: com.apple.main-thread

Any time you see EXC_BAD_ACCESS your first thought should be 'I've over released some object'.

As @csch said, you should turn on zombies and see what info that tells you.

My guess is that iconsMutableArray is initialized via a slice of code like this;

iconsMutableArray = [NSMutableArray array];

which assigns but does not retain (read the memory management guide here).

Also you should be using gesture recognizers instead of implementing the touch handling yourself. Read about that here.

answered on Stack Overflow May 16, 2011 by Bill Dudney
0

Looks like iconsMutableArray got released somewhere, or was never set. Maybe you set it to an autoreleased object and forgot to retain it?

answered on Stack Overflow May 16, 2011 by Eric Petroelje
0

I recommend rewriting the code in multiple lines and then step through with the debugger to see if either the image or the array is nil. It's also possible that you are accessing an index that is out of bounds.

Also set NSZombieEnabled to YES as described here. This might give you are more detailed error message.

answered on Stack Overflow May 16, 2011 by csch

User contributions licensed under CC BY-SA 3.0