iOS exception in pushViewController - objective-c

I'm trying to create an NSMutableArray of ModuleViewControllers (which inherits from UIViewController) in order to open a given view from a given button. The ModuleViewController is also described in ModuleViewController.xib
So I load a list of module xml files and pass them, one by one, to a constructor (which ignores them atm) and then add the result to an NSMutableArray.
The problem happens when I try to push the view controller. I get a SIGABRT/exception and I'm not sure why. Any ideas?
Failed Call:
-(IBAction) buttonPressed:(id)sender {
ModuleViewController *module = [moduleVCList objectAtIndex:[sender tag]];
[[self navigationController] pushViewController:module animated:YES];
}
Constructor Call:
[moduleVCList addObject:
[[ModuleViewController alloc] initWithXMLName:[module objectForKey:#"file"]]];
Constructor:
- (id)initWithXMLName:(NSString *)xmlName {
self = [super initWithNibName:#"ModuleViewController" bundle:nil];
if (self) {
}
return self;
}
Backtrace:
#0 0x01867cf0 in objc_exception_throw ()
#1 0x0167ea78 in +[NSException raise:format:arguments:] ()
#2 0x0167e9e9 in +[NSException raise:format:] ()
#3 0x00209f0d in -[UIViewController _loadViewFromNibNamed:bundle:] ()
#4 0x0020a3a9 in -[UIViewController loadView] ()
#5 0x0020a5cb in -[UIViewController view] ()
#6 0x0020a941 in -[UIViewController contentScrollView] ()
#7 0x0021c47d in -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] ()
#8 0x0021c66f in -[UINavigationController _layoutViewController:] ()
#9 0x0021c93b in -[UINavigationController _startTransition:fromViewController:toViewController:] ()
#10 0x0021d3df in -[UINavigationController _startDeferredTransitionIfNeeded] ()
#11 0x0021d986 in -[UINavigationController pushViewController:transition:forceImmediate:] ()
#12 0x0021d5a4 in -[UINavigationController pushViewController:animated:] ()
#13 0x00003c1a in -[MainViewController buttonPressed:] (self=0x6a283d0, _cmd=0xb874, sender=0x6a286c0) at *snip*/MainViewController.m:149
#14 0x016d7ec9 in -[NSObject performSelector:withObject:withObject:] ()
#15 0x001475c2 in -[UIApplication sendAction:to:from:forEvent:] ()
#16 0x0014755a in -[UIApplication sendAction:toTarget:fromSender:forEvent:] ()
#17 0x001ecb76 in -[UIControl sendAction:to:forEvent:] ()
#18 0x001ed03f in -[UIControl(Internal) _sendActionsForEvents:withEvent:] ()
#19 0x001ec2fe in -[UIControl touchesEnded:withEvent:] ()
#20 0x0016ca30 in -[UIWindow _sendTouchesForEvent:] ()
#21 0x0016cc56 in -[UIWindow sendEvent:] ()
#22 0x00153384 in -[UIApplication sendEvent:] ()
#23 0x00146aa9 in _UIApplicationHandleEvent ()
#24 0x015c0fa9 in PurpleEventCallback ()
#25 0x016aa1c5 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
#26 0x0160f022 in __CFRunLoopDoSource1 ()
#27 0x0160d90a in __CFRunLoopRun ()
#28 0x0160cdb4 in CFRunLoopRunSpecific ()
#29 0x0160cccb in CFRunLoopRunInMode ()
#30 0x015bf879 in GSEventRunModal ()
#31 0x015bf93e in GSEventRun ()
#32 0x00144a9b in UIApplicationMain ()
#33 0x00002862 in main (argc=1, argv=0xbfffedc8) at *snip*/main.m:16
#34 0x000027d5 in start ()

Related

Get web text to Label

I need to get the text from URL http://cast.midiaip.com.br:6530/currentsong?sid=1 and display it in a UILabel
My code is like that:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *musicasLabel;
- (IBAction)textoButton:(id)sender;
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize musicasLabel;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)textoButton:(id)sender {
NSURL *url = [NSURL URLWithString:#"http://cast.midiaip.com.br:6530/currentsong?sid=1"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *string = [NSString stringWithUTF8String:[data bytes]];
musicasLabel.text = string;
}
#end
I always get the error:
2015-04-17 02:55:21.637 teste label[14194:797672] ***
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '***
+[NSString stringWithUTF8String:]: NULL cString'
*** First throw call stack:
(
0 CoreFoundation 0x000000010cf1bf35
__exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010cbb4bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010cf1be6d +[NSException raise:format:] + 205
3 Foundation 0x000000010c719464 +[NSString stringWithUTF8String:] + 78
4 teste label 0x000000010c6885e5 -[ViewController textoButton:] + 181
5 UIKit 0x000000010d3098be -[UIApplication sendAction:to:from:forEvent:] + 75
6 UIKit 0x000000010d410410 -[UIControl _sendActionsForEvents:withEvent:] + 467
7 UIKit 0x000000010d40f7df -[UIControl touchesEnded:withEvent:] + 522
8 UIKit 0x000000010d34f308 -[UIWindow _sendTouchesForEvent:] + 735
9 UIKit 0x000000010d34fc33 -[UIWindow sendEvent:] + 683
10 UIKit 0x000000010d31c9b1 -[UIApplication sendEvent:] + 246
11 UIKit 0x000000010d329a7d _UIApplicationHandleEventFromQueueEvent + 17370
12 UIKit 0x000000010d305103 _UIApplicationHandleEventQueue + 1961
13 CoreFoundation 0x000000010ce51551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14 CoreFoundation 0x000000010ce4741d __CFRunLoopDoSources0 + 269
15 CoreFoundation 0x000000010ce46a54 __CFRunLoopRun + 868
16 CoreFoundation 0x000000010ce46486 CFRunLoopRunSpecific + 470
17 GraphicsServices 0x00000001104ea9f0 GSEventRunModal + 161
18 UIKit 0x000000010d308420 UIApplicationMain + 1282
19 teste label 0x000000010c688ae3 main + 115
20 libdyld.dylib 0x000000010f4ab145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Thank you very much!!!
You are initializing your string incorrectly. Your question is answered here:
convert UTF-8 encoded NSData to a NSString

Cocoa Bindings to new NSManagedObject

I am new to this, so I will lay out what I am doing and please correct any mistakes.
In my AppDelegate, I have a #property for an NSManagedObject subclass, "Entry". When I press a button on the main window to create a new Entry, I first instantiate the property:
_entry = (Entry*)[NSEntityDescription
insertNewObjectForEntityForName:#"Entry"
inManagedObjectContext:[self managedObjectContext]];
then I open up a new window by calling
[inputWindow makeKeyAndOrderFront:nil];
This inputWindow has Cocoa Bindings to the entry's properties. When I submit, I do the usual save
[[self managedObjectContext] save:&error];
and then
[inputWindow performClose:nil];
This seems pretty straight forward, but I get an error:
2014-01-11 22:57:32.906 WriteOffs[66441:303] CoreData: error: Failed to call designated initializer on NSManagedObject class 'Entry'
2014-01-11 22:57:32.943 WriteOffs[66441:303] An uncaught exception was raised
2014-01-11 22:57:32.943 WriteOffs[66441:303] [<Entry 0x6100000ba040> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "date".
It seems that entry is null, implying that the inputWindow and its bindings are instantiated before I click the button on the main window to create the new entry and open up the inputWindow.
Seems like there is a simple solution that I am missing.
EDIT:
Here's what I have. In WOAppDelegate.h,
#import <Cocoa/Cocoa.h>
#import "Entry.h"
#interface WOAppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet NSWindow *inputWindow;
#property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
#property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
#property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
- (IBAction)saveAction:(id)sender;
#property (weak, nonatomic) Entry *entry;
#end
Then in WOAppDelegate.m
#import "WOAppDelegate.h"
#implementation WOAppDelegate
#synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
#synthesize managedObjectModel = _managedObjectModel;
#synthesize managedObjectContext = _managedObjectContext;
#synthesize inputWindow = _inputWindow;
#synthesize entry = _entry;
// Core Data Stuff...
Entry.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#interface Entry : NSManagedObject
#property (nonatomic, retain) NSNumber * amount;
#property (nonatomic, retain) NSString * category;
#property (nonatomic, retain) NSDate * date;
#property (nonatomic, retain) NSString * payee;
#property (nonatomic, retain) NSString * text;
#end
Entry.m
#import "Entry.h"
#implementation Entry
#dynamic amount;
#dynamic category;
#dynamic date;
#dynamic images;
#dynamic payee;
#dynamic text;
#end
Pretty simple. In my MainMenu.xib, I have the main window, and the inputWindow. I have an array controller binded to the managedObjectContext that fills in content for the table on the main window. I also have a second window for entering in new entries to the table. This is bonded to the property, entry.
Thanks again for the help,
Chet
Does your “Entry” class maybe override some of the methods in NSManagedObject? Maybe even
- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context;
If so, do you fail to call -super in any of your subclass’s methods?
In the array controller, I had mode:Class referencing the Entry class. I changed that to mode:Entity referencing the Entry entity and everything worked out. :)
Can someone comment on why the debugger is so bad at giving a meaningful response? Where in the error message could have pointed me to this being the error?
2014-01-12 18:35:31.520 WriteOffs[68395:303] CoreData: error: Failed to call designated initializer on NSManagedObject class 'Entry'
2014-01-12 18:35:31.555 WriteOffs[68395:303] An uncaught exception was raised
2014-01-12 18:35:31.555 WriteOffs[68395:303] [<Entry 0x6100000a9a20> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "date".
2014-01-12 18:35:31.556 WriteOffs[68395:303] (
0 CoreFoundation 0x00007fff8b93f41c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff944f3e75 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8b93efc9 -[NSException raise] + 9
3 CoreData 0x00007fff89650b14 -[NSManagedObject valueForUndefinedKey:] + 276
4 Foundation 0x00007fff91eff49f -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 334
5 AppKit 0x00007fff89c872d3 -[NSBinder valueForBinding:atIndex:resolveMarkersToPlaceholders:] + 408
6 AppKit 0x00007fff89963567 -[NSValueBinder _adjustObject:mode:observedController:observedKeyPath:context:editableState:adjustState:] + 622
7 AppKit 0x00007fff8a022008 -[NSValueBinder updateTableColumnDataCell:forDisplayAtIndex:] + 143
8 AppKit 0x00007fff89f9e2ee -[NSTextValueBinder updateTableColumnDataCell:forDisplayAtIndex:] + 73
9 CoreFoundation 0x00007fff8b873639 __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 137
10 CoreFoundation 0x00007fff8b872ddf -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 319
11 AppKit 0x00007fff8a062ac7 -[_NSBindingAdaptor tableColumn:willDisplayCell:row:] + 108
12 AppKit 0x00007fff89b8ada0 -[NSTableView preparedCellAtColumn:row:] + 615
13 AppKit 0x00007fff89b8a9ee -[NSTableView _drawContentsAtRow:column:withCellFrame:] + 44
14 AppKit 0x00007fff89b8a723 -[NSTableView drawRow:clipRect:] + 1629
15 AppKit 0x00007fff89b89f7d -[NSTableView drawRowIndexes:clipRect:] + 776
16 AppKit 0x00007fff89a534c2 -[NSTableView drawRect:] + 1484
17 AppKit 0x00007fff89a2c399 -[NSView _drawRect:clip:] + 3748
18 AppKit 0x00007fff89a2ac0e -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 1799
19 AppKit 0x00007fff89a2afea -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2787
20 AppKit 0x00007fff89a2afea -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2787
21 AppKit 0x00007fff89a2afea -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2787
22 AppKit 0x00007fff89a2afea -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2787
23 AppKit 0x00007fff89a28a50 -[NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 841
24 AppKit 0x00007fff89a28201 -[NSThemeFrame _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 314
25 AppKit 0x00007fff89a25209 -[NSView _displayRectIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:] + 2828
26 AppKit 0x00007fff89a0463a -[NSView displayIfNeeded] + 1680
27 AppKit 0x00007fff89a1e246 -[NSWindow _reallyDoOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 1972
28 AppKit 0x00007fff89a1d7f0 -[NSWindow _doOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 786
29 AppKit 0x00007fff89a1d470 -[NSWindow orderWindow:relativeTo:] + 162
30 AppKit 0x00007fff898bbc26 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1406
31 AppKit 0x00007fff898b05fe loadNib + 327
32 AppKit 0x00007fff898afb9c +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 288
33 AppKit 0x00007fff898af98c -[NSBundle(NSNibLoading) loadNibNamed:owner:topLevelObjects:] + 197
34 AppKit 0x00007fff898af77b +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 357
35 AppKit 0x00007fff898ab617 NSApplicationMain + 448
36 WriteOffs 0x0000000100002e02 main + 34
37 libdyld.dylib 0x00007fff8eddf5fd start + 1
38 ??? 0x0000000000000003 0x0 + 3
)
2014-01-12 18:35:31.640 WriteOffs[68395:303] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Entry 0x6100000a9a20> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "date".'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8b93f41c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff944f3e75 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8b93efc9 -[NSException raise] + 9
3 CoreData 0x00007fff89650b14 -[NSManagedObject valueForUndefinedKey:] + 276
4 Foundation 0x00007fff91eff49f -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 334
5 AppKit 0x00007fff89c872d3 -[NSBinder valueForBinding:atIndex:resolveMarkersToPlaceholders:] + 408
6 AppKit 0x00007fff89963567 -[NSValueBinder _adjustObject:mode:observedController:observedKeyPath:context:editableState:adjustState:] + 622
7 AppKit 0x00007fff8a022008 -[NSValueBinder updateTableColumnDataCell:forDisplayAtIndex:] + 143
8 AppKit 0x00007fff89f9e2ee -[NSTextValueBinder updateTableColumnDataCell:forDisplayAtIndex:] + 73
9 CoreFoundation 0x00007fff8b873639 __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 137
10 CoreFoundation 0x00007fff8b872ddf -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 319
11 AppKit 0x00007fff8a062ac7 -[_NSBindingAdaptor tableColumn:willDisplayCell:row:] + 108
12 AppKit 0x00007fff89b8ada0 -[NSTableView preparedCellAtColumn:row:] + 615
13 AppKit 0x00007fff89b8a9ee -[NSTableView _drawContentsAtRow:column:withCellFrame:] + 44
14 AppKit 0x00007fff89b8a723 -[NSTableView drawRow:clipRect:] + 1629
15 AppKit 0x00007fff89b89f7d -[NSTableView drawRowIndexes:clipRect:] + 776
16 AppKit 0x00007fff89a534c2 -[NSTableView drawRect:] + 1484
17 AppKit 0x00007fff89a2c399 -[NSView _drawRect:clip:] + 3748
18 AppKit 0x00007fff89a2ac0e -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 1799
19 AppKit 0x00007fff89a2afea -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2787
20 AppKit 0x00007fff89a2afea -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2787
21 AppKit 0x00007fff89a2afea -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2787
22 AppKit 0x00007fff89a2afea -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2787
23 AppKit 0x00007fff89a28a50 -[NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 841
24 AppKit 0x00007fff89a28201 -[NSThemeFrame _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 314
25 AppKit 0x00007fff89a25209 -[NSView _displayRectIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:] + 2828
26 AppKit 0x00007fff89a0463a -[NSView displayIfNeeded] + 1680
27 AppKit 0x00007fff89a1e246 -[NSWindow _reallyDoOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 1972
28 AppKit 0x00007fff89a1d7f0 -[NSWindow _doOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 786
29 AppKit 0x00007fff89a1d470 -[NSWindow orderWindow:relativeTo:] + 162
30 AppKit 0x00007fff898bbc26 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1406
31 AppKit 0x00007fff898b05fe loadNib + 327
32 AppKit 0x00007fff898afb9c +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 288
33 AppKit 0x00007fff898af98c -[NSBundle(NSNibLoading) loadNibNamed:owner:topLevelObjects:] + 197
34 AppKit 0x00007fff898af77b +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 357
35 AppKit 0x00007fff898ab617 NSApplicationMain + 448
36 WriteOffs 0x0000000100002e02 main + 34
37 libdyld.dylib 0x00007fff8eddf5fd start + 1
38 ??? 0x0000000000000003 0x0 + 3
)
libc++abi.dylib: terminating with uncaught exception of type NSException

[Image count]: unrecognized selector sent to instance

I get the following error when saving managedObjectContext:
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[Image count]: unrecognized selector sent to instance
Code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo {
// Delete any existing image.
Image *oldImage = _place.image;
if (oldImage != nil) {
[_place.managedObjectContext deleteObject:oldImage];
}
Image *image = [NSEntityDescription insertNewObjectForEntityForName:#"Image" inManagedObjectContext:_place.managedObjectContext];
image.imageData = UIImageJPEGRepresentation(selectedImage, 0.5);
_place.image = image;
[_place.managedObjectContext save:nil]; //Here is the crash
[self dismissViewControllerAnimated:YES completion:nil];
Image entity has two one-to-one relationships: place and trip.
#interface Image : NSManagedObject
#property (nonatomic, retain) NSData * imageData;
#property (nonatomic, retain) Trip *trip;
#property (nonatomic, retain) Place *place;
#end
Can't understand why [Image count] error comes out on one-to-one relationship.
Upd.:
#interface Place : NSManagedObject
#property (nonatomic, retain) NSString * address;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) id thumbnail;
#property (nonatomic, retain) Image *image;
#end
Upd.2:
*** First throw call stack:
(
0 CoreFoundation 0x022935e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01cef8b6 objc_exception_throw + 44
2 CoreFoundation 0x02330903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0228390b ___forwarding___ + 1019
4 CoreFoundation 0x022834ee _CF_forwarding_prep_0 + 14
5 CoreData 0x00803df8 -[NSSQLCore _knownOrderKeyForObject:from:inverseToMany:] + 200
6 CoreData 0x0073ad50 -[NSSQLCore _populateRowForOp:withObject:] + 1120
7 CoreData 0x00750e27 -[NSSQLCore recordValuesForInsertedObject:] + 71
8 CoreData 0x00739b5d -[NSSQLCore recordChangesInContext:] + 685
9 CoreData 0x00738925 -[NSSQLCore saveChanges:] + 565
10 CoreData 0x0070555c -[NSSQLCore executeRequest:withContext:error:] + 412
11 CoreData 0x00705050 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 4704
12 CoreData 0x00731ccc -[NSManagedObjectContext save:] + 764
13 Travel Plan 0x0001cf12 -[PlaceViewController imagePickerController:didFinishPickingImage:editingInfo:] + 738
14 UIKit 0x00cd5e6c -[UIImagePickerController _imagePickerDidCompleteWithInfo:] + 488
15 PhotoLibrary 0x13694e94 PLNotifyImagePickerOfImageAvailability + 106
16 PhotosUI 0x14411585 -[PUUIPhotosAlbumViewController handleNavigateToAsset:inContainer:] + 401
17 PhotosUI 0x143947b4 -[PUPhotosGridViewController collectionView:shouldSelectItemAtIndexPath:] + 577
18 UIKit 0x0106dc0b -[UICollectionView _selectItemAtIndexPath:animated:scrollPosition:notifyDelegate:] + 173
19 UIKit 0x010861f8 -[UICollectionView _userSelectItemAtIndexPath:] + 189
20 UIKit 0x010863b5 -[UICollectionView touchesEnded:withEvent:] + 437
21 libobjc.A.dylib 0x01d01874 -[NSObject performSelector:withObject:withObject:] + 77
22 UIKit 0x00bb6902 forwardTouchMethod + 271
23 UIKit 0x00bb6972 -[UIResponder touchesEnded:withEvent:] + 30
24 libobjc.A.dylib 0x01d01874 -[NSObject performSelector:withObject:withObject:] + 77
25 UIKit 0x00bb6902 forwardTouchMethod + 271
26 UIKit 0x00bb6972 -[UIResponder touchesEnded:withEvent:] + 30
27 libobjc.A.dylib 0x01d01874 -[NSObject performSelector:withObject:withObject:] + 77
28 UIKit 0x00bb6902 forwardTouchMethod + 271
29 UIKit 0x00bb6972 -[UIResponder touchesEnded:withEvent:] + 30
30 UIKit 0x00dd1c7f _UIGestureRecognizerUpdate + 7166
31 UIKit 0x00a9c19a -[UIWindow _sendGesturesForEvent:] + 1291
32 UIKit 0x00a9d0ba -[UIWindow sendEvent:] + 1030
33 UIKit 0x00a70e86 -[UIApplication sendEvent:] + 242
34 UIKit 0x00a5b18f _UIApplicationHandleEventQueue + 11421
35 CoreFoundation 0x0221c83f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
36 CoreFoundation 0x0221c1cb __CFRunLoopDoSources0 + 235
37 CoreFoundation 0x0223929e __CFRunLoopRun + 910
38 CoreFoundation 0x02238ac3 CFRunLoopRunSpecific + 467
39 CoreFoundation 0x022388db CFRunLoopRunInMode + 123
40 GraphicsServices 0x046299e2 GSEventRunModal + 192
41 GraphicsServices 0x04629809 GSEventRun + 104
42 UIKit 0x00a5dd3b UIApplicationMain + 1225
43 Travel Plan 0x0004c53d main + 141
44 libdyld.dylib 0x02dd170d start + 1
)
I got exactly the same problem after changing some relations in my models. This seems to be a bug in Xcode that appears when you change a relationship that was "to many : ordered" to a "to one" relation. It's like Xcode still thinks that the relation is to many because the "ordered" tick-box is ticked.
A very easy solution to solve this is to change back to a "to many" relationship, unselect "ordered" and then change it back again to a "to one" relation.
That was some bug in Xcode. After setting relationship to many, and then back to one, it started to work without errors.

NSOperationQueue bug with dependencies

I am using NSOperation and NSOperationQueue for performing a sequence of operations, all dependent on each other (2 on 1, 3 on 2, etc...). I set the dependency after I create the operations. I am encountering problems when the queue completes: the program crashes in the _release part of the code, apparently when the NSOperations are getting released. Note that they all get released at the end by the queue, because it is only after the very last one which depends on the second last one, which depends on etc... that they can be released. If I remove any dependency, the code runs fine. If I change waitUntilFinished: to NO, it crashes, if it is YES, it does not.
I have isolated the problem to the following code which does not use any of my custom classes. NSOperation by default is a class that does absolutely nothing. Yet, this still crashes when all operations have completed. Therefore, it appears I am not using NSOperationQueue properly but can't see what is wrong. I am running on 10.9 and I have noticed that in general Maverick 10.9 is much more sensitive to these issues than 10.8.
I call this method from the main Thread with a Menu item:
- (void) testOperations:(id)object
{
NSOperationQueue* queue = [ [ NSOperationQueue alloc ] init ];
NSMutableArray* array = [ NSMutableArray array ];
for ( int i = 0; i < 10000; i++)
[ array addObject: [[[ NSOperation alloc ] init ] autorelease ] ];
for ( int i = 1; i < [ array count ]; i++)
[[ array objectAtIndex:i ] addDependency:[array objectAtIndex:i-1]]; // remove this and no crash
[ queue addOperations: array waitUntilFinished:NO ]; // Change to YES, no crash
[ queue autorelease ]; // or release, it does not make a difference, in fact leaking the memory makes no difference: the code crashes when the queue is removing the NSOperations
}
This will crash every single time with:
bool objc::DenseMapBase >, objc_object*, unsigned long, objc::DenseMapInfo, true>: (EXC_BAD_ACCESS)
The full stack is:
#0 0x9104d81b in objc::DenseMapBase<objc::DenseMap<objc_object*, unsigned long, true, objc::DenseMapInfo<objc_object*> >, objc_object*, unsigned long, objc::DenseMapInfo<objc_object*>, true>::find(objc_object* const&) ()
#1 0x910384e3 in _objc_rootReleaseWasZero ()
#2 0x9104d5d9 in -[NSObject release] ()
#3 0x99e41224 in CFRelease ()
#4 0x99e56277 in -[__NSArrayM dealloc] ()
#5 0x9104d5ef in -[NSObject release] ()
#6 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#7 0x9104d5ef in -[NSObject release] ()
#8 0x97f62ac8 in -[NSOperation dealloc] ()
#9 0x9104d5ef in -[NSObject release] ()
#10 0x99e41224 in CFRelease ()
#11 0x99e56277 in -[__NSArrayM dealloc] ()
#12 0x9104d5ef in -[NSObject release] ()
#13 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#14 0x9104d5ef in -[NSObject release] ()
#15 0x97f62ac8 in -[NSOperation dealloc] ()
#16 0x9104d5ef in -[NSObject release] ()
#17 0x99e41224 in CFRelease ()
#18 0x99e56277 in -[__NSArrayM dealloc] ()
#19 0x9104d5ef in -[NSObject release] ()
#20 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#21 0x9104d5ef in -[NSObject release] ()
#22 0x97f62ac8 in -[NSOperation dealloc] ()
#23 0x9104d5ef in -[NSObject release] ()
#24 0x99e41224 in CFRelease ()
#25 0x99e56277 in -[__NSArrayM dealloc] ()
#26 0x9104d5ef in -[NSObject release] ()
#27 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#28 0x9104d5ef in -[NSObject release] ()
#29 0x97f62ac8 in -[NSOperation dealloc] ()
#30 0x9104d5ef in -[NSObject release] ()
#31 0x99e41224 in CFRelease ()
#32 0x99e56277 in -[__NSArrayM dealloc] ()
#33 0x9104d5ef in -[NSObject release] ()
#34 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#35 0x9104d5ef in -[NSObject release] ()
#36 0x97f62ac8 in -[NSOperation dealloc] ()
#37 0x9104d5ef in -[NSObject release] ()
#38 0x99e41224 in CFRelease ()
#39 0x99e56277 in -[__NSArrayM dealloc] ()
#40 0x9104d5ef in -[NSObject release] ()
#41 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#42 0x9104d5ef in -[NSObject release] ()
#43 0x97f62ac8 in -[NSOperation dealloc] ()
#44 0x9104d5ef in -[NSObject release] ()
#45 0x99e41224 in CFRelease ()
#46 0x99e56277 in -[__NSArrayM dealloc] ()
#47 0x9104d5ef in -[NSObject release] ()
#48 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#49 0x9104d5ef in -[NSObject release] ()
#50 0x97f62ac8 in -[NSOperation dealloc] ()
#10722 0x9104d5ef in -[NSObject release] ()
#10723 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10724 0x9104d5ef in -[NSObject release] ()
#10725 0x97f62ac8 in -[NSOperation dealloc] ()
#10726 0x9104d5ef in -[NSObject release] ()
#10727 0x99e41224 in CFRelease ()
#10728 0x99e56277 in -[__NSArrayM dealloc] ()
#10729 0x9104d5ef in -[NSObject release] ()
#10730 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10731 0x9104d5ef in -[NSObject release] ()
#10732 0x97f62ac8 in -[NSOperation dealloc] ()
#10733 0x9104d5ef in -[NSObject release] ()
#10734 0x99e41224 in CFRelease ()
#10735 0x99e56277 in -[__NSArrayM dealloc] ()
#10736 0x9104d5ef in -[NSObject release] ()
#10737 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10738 0x9104d5ef in -[NSObject release] ()
#10739 0x97f62ac8 in -[NSOperation dealloc] ()
#10740 0x9104d5ef in -[NSObject release] ()
#10741 0x99e41224 in CFRelease ()
#10742 0x99e56277 in -[__NSArrayM dealloc] ()
#10743 0x9104d5ef in -[NSObject release] ()
#10744 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10745 0x9104d5ef in -[NSObject release] ()
#10746 0x97f62ac8 in -[NSOperation dealloc] ()
#10747 0x9104d5ef in -[NSObject release] ()
#10748 0x99e41224 in CFRelease ()
#10749 0x99e56277 in -[__NSArrayM dealloc] ()
#10750 0x9104d5ef in -[NSObject release] ()
#10751 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10752 0x9104d5ef in -[NSObject release] ()
#10753 0x97f62ac8 in -[NSOperation dealloc] ()
#10754 0x9104d5ef in -[NSObject release] ()
#10755 0x99e41224 in CFRelease ()
#10756 0x99e56277 in -[__NSArrayM dealloc] ()
#10757 0x9104d5ef in -[NSObject release] ()
#10758 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10759 0x9104d5ef in -[NSObject release] ()
#10760 0x97f62ac8 in -[NSOperation dealloc] ()
#10761 0x9104d5ef in -[NSObject release] ()
#10762 0x99e41224 in CFRelease ()
#10763 0x99e56277 in -[__NSArrayM dealloc] ()
#10764 0x9104d5ef in -[NSObject release] ()
#10765 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10766 0x9104d5ef in -[NSObject release] ()
#10767 0x97f62ac8 in -[NSOperation dealloc] ()
#10768 0x9104d5ef in -[NSObject release] ()
#10769 0x99e41224 in CFRelease ()
#10770 0x99e56277 in -[__NSArrayM dealloc] ()
#10771 0x9104d5ef in -[NSObject release] ()
#10772 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10773 0x9104d5ef in -[NSObject release] ()
#10774 0x97f62ac8 in -[NSOperation dealloc] ()
#10775 0x9104d5ef in -[NSObject release] ()
#10776 0x99e41224 in CFRelease ()
#10777 0x99e56277 in -[__NSArrayM dealloc] ()
#10778 0x9104d5ef in -[NSObject release] ()
#10779 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10780 0x9104d5ef in -[NSObject release] ()
#10781 0x97f62ac8 in -[NSOperation dealloc] ()
#10782 0x9104d5ef in -[NSObject release] ()
#10783 0x99e41224 in CFRelease ()
#10784 0x99e56277 in -[__NSArrayM dealloc] ()
#10785 0x9104d5ef in -[NSObject release] ()
#10786 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10787 0x9104d5ef in -[NSObject release] ()
#10788 0x97f62ac8 in -[NSOperation dealloc] ()
#10789 0x9104d5ef in -[NSObject release] ()
#10790 0x99e41224 in CFRelease ()
#10791 0x99e56277 in -[__NSArrayM dealloc] ()
#10792 0x9104d5ef in -[NSObject release] ()
#10793 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10794 0x9104d5ef in -[NSObject release] ()
#10795 0x97f62ac8 in -[NSOperation dealloc] ()
#10796 0x9104d5ef in -[NSObject release] ()
#10797 0x99e41224 in CFRelease ()
#10798 0x99e56277 in -[__NSArrayM dealloc] ()
#10799 0x9104d5ef in -[NSObject release] ()
#10800 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10801 0x9104d5ef in -[NSObject release] ()
#10802 0x97f62ac8 in -[NSOperation dealloc] ()
#10803 0x9104d5ef in -[NSObject release] ()
#10804 0x99e41224 in CFRelease ()
#10805 0x99e56277 in -[__NSArrayM dealloc] ()
#10806 0x9104d5ef in -[NSObject release] ()
#10807 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10808 0x9104d5ef in -[NSObject release] ()
#10809 0x97f62ac8 in -[NSOperation dealloc] ()
#10810 0x9104d5ef in -[NSObject release] ()
#10811 0x99e41224 in CFRelease ()
#10812 0x99e56277 in -[__NSArrayM dealloc] ()
#10813 0x9104d5ef in -[NSObject release] ()
#10814 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10815 0x9104d5ef in -[NSObject release] ()
#10816 0x97f62ac8 in -[NSOperation dealloc] ()
#10817 0x9104d5ef in -[NSObject release] ()
#10818 0x99e41224 in CFRelease ()
#10819 0x99e56277 in -[__NSArrayM dealloc] ()
#10820 0x9104d5ef in -[NSObject release] ()
#10821 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10822 0x9104d5ef in -[NSObject release] ()
#10823 0x97f62ac8 in -[NSOperation dealloc] ()
#10824 0x9104d5ef in -[NSObject release] ()
#10825 0x99e41224 in CFRelease ()
#10826 0x99e56277 in -[__NSArrayM dealloc] ()
#10827 0x9104d5ef in -[NSObject release] ()
#10828 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10829 0x9104d5ef in -[NSObject release] ()
#10830 0x97f62ac8 in -[NSOperation dealloc] ()
#10831 0x9104d5ef in -[NSObject release] ()
#10832 0x99e41224 in CFRelease ()
#10833 0x99e56277 in -[__NSArrayM dealloc] ()
#10834 0x9104d5ef in -[NSObject release] ()
#10835 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10836 0x9104d5ef in -[NSObject release] ()
#10837 0x97f62ac8 in -[NSOperation dealloc] ()
#10838 0x9104d5ef in -[NSObject release] ()
#10839 0x99e41224 in CFRelease ()
#10840 0x99e56277 in -[__NSArrayM dealloc] ()
#10841 0x9104d5ef in -[NSObject release] ()
#10842 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10843 0x9104d5ef in -[NSObject release] ()
#10844 0x97f62ac8 in -[NSOperation dealloc] ()
#10845 0x9104d5ef in -[NSObject release] ()
#10846 0x99e41224 in CFRelease ()
#10847 0x99e56277 in -[__NSArrayM dealloc] ()
#10848 0x9104d5ef in -[NSObject release] ()
#10849 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10850 0x9104d5ef in -[NSObject release] ()
#10851 0x97f62ac8 in -[NSOperation dealloc] ()
#10852 0x9104d5ef in -[NSObject release] ()
#10853 0x99e41224 in CFRelease ()
#10854 0x99e56277 in -[__NSArrayM dealloc] ()
#10855 0x9104d5ef in -[NSObject release] ()
#10856 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10857 0x9104d5ef in -[NSObject release] ()
#10858 0x97f62ac8 in -[NSOperation dealloc] ()
#10859 0x9104d5ef in -[NSObject release] ()
#10860 0x99e41224 in CFRelease ()
#10861 0x99e56277 in -[__NSArrayM dealloc] ()
#10862 0x9104d5ef in -[NSObject release] ()
#10863 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10864 0x9104d5ef in -[NSObject release] ()
#10865 0x97f62ac8 in -[NSOperation dealloc] ()
#10866 0x9104d5ef in -[NSObject release] ()
#10867 0x99e41224 in CFRelease ()
#10868 0x99e56277 in -[__NSArrayM dealloc] ()
#10869 0x9104d5ef in -[NSObject release] ()
#10870 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10871 0x9104d5ef in -[NSObject release] ()
#10872 0x97f62ac8 in -[NSOperation dealloc] ()
#10873 0x9104d5ef in -[NSObject release] ()
#10874 0x99e41224 in CFRelease ()
#10875 0x99e56277 in -[__NSArrayM dealloc] ()
#10876 0x9104d5ef in -[NSObject release] ()
#10877 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10878 0x9104d5ef in -[NSObject release] ()
#10879 0x97f62ac8 in -[NSOperation dealloc] ()
#10880 0x9104d5ef in -[NSObject release] ()
#10881 0x99e41224 in CFRelease ()
#10882 0x99e56277 in -[__NSArrayM dealloc] ()
#10883 0x9104d5ef in -[NSObject release] ()
#10884 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10885 0x9104d5ef in -[NSObject release] ()
#10886 0x97f62ac8 in -[NSOperation dealloc] ()
#10887 0x9104d5ef in -[NSObject release] ()
#10888 0x99e41224 in CFRelease ()
#10889 0x99e56277 in -[__NSArrayM dealloc] ()
#10890 0x9104d5ef in -[NSObject release] ()
#10891 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10892 0x9104d5ef in -[NSObject release] ()
#10893 0x97f62ac8 in -[NSOperation dealloc] ()
#10894 0x9104d5ef in -[NSObject release] ()
#10895 0x99e41224 in CFRelease ()
#10896 0x99e56277 in -[__NSArrayM dealloc] ()
#10897 0x9104d5ef in -[NSObject release] ()
#10898 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10899 0x9104d5ef in -[NSObject release] ()
#10900 0x97f62ac8 in -[NSOperation dealloc] ()
#10901 0x9104d5ef in -[NSObject release] ()
#10902 0x99e41224 in CFRelease ()
#10903 0x99e56277 in -[__NSArrayM dealloc] ()
#10904 0x9104d5ef in -[NSObject release] ()
#10905 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10906 0x9104d5ef in -[NSObject release] ()
#10907 0x97f62ac8 in -[NSOperation dealloc] ()
#10908 0x9104d5ef in -[NSObject release] ()
#10909 0x99e41224 in CFRelease ()
#10910 0x99e56277 in -[__NSArrayM dealloc] ()
#10911 0x9104d5ef in -[NSObject release] ()
#10912 0x97f62b22 in -[__NSOperationInternal dealloc] ()
#10913 0x9104d5ef in -[NSObject release] ()
#10914 0x97f62ac8 in -[NSOperation dealloc] ()
#10915 0x9104d5ef in -[NSObject release] ()
#10916 0x97f49cca in __NSOQSchedule_f ()
#10917 0x9c1c9e21 in _dispatch_async_redirect_invoke ()
#10918 0x9c1c53a6 in _dispatch_client_callout ()
#10919 0x9c1c7467 in _dispatch_root_queue_drain ()
#10920 0x9c1c8732 in _dispatch_worker_thread2 ()
#10921 0x960c2dab in _pthread_wqthread ()
The full crash context is (bold for crash line):
libobjc.A.dylib`objc::DenseMapBase<objc::DenseMap<objc_object*, unsigned long, true, objc::DenseMapInfo<objc_object*> >, objc_object*, unsigned long, objc::DenseMapInfo<objc_object*>, true>::find(objc_object* const&):
0x9104d800: pushl %ebp
0x9104d801: movl %esp, %ebp
0x9104d803: pushl %esi
0x9104d804: subl $20, %esp
0x9104d807: leal -8(%ebp), %eax
0x9104d80a: movl %eax, 8(%esp)
0x9104d80e: movl 16(%ebp), %eax
0x9104d811: movl %eax, 4(%esp)
0x9104d815: movl 12(%ebp), %esi
0x9104d818: movl %esi, (%esp)
**0x9104d81b: calll 0x9104d9b6 ; bool objc::DenseMapBase<objc::DenseMap<objc_object*, unsigned long, true, objc::DenseMapInfo<objc_object*> >, objc_object*, unsigned long, objc::DenseMapInfo<objc_object*>, true>::LookupBucketFor<objc_object*>(objc_object* const&, std::__1::pair<objc_object*, unsigned long> const*&) const**
0x9104d820: movl 12(%esi), %ecx
0x9104d823: shll $3, %ecx
0x9104d826: addl (%esi), %ecx
0x9104d828: movl 8(%ebp), %edx
0x9104d82b: testb %al, %al
0x9104d82d: je 0x9104d836 ; objc::DenseMapBase<objc::DenseMap<objc_object*, unsigned long, true, objc::DenseMapInfo<objc_object*> >, objc_object*, unsigned long, objc::DenseMapInfo<objc_object*>, true>::find(objc_object* const&) + 54
0x9104d82f: movl -8(%ebp), %eax
0x9104d832: movl %eax, (%edx)
0x9104d834: jmp 0x9104d838 ; objc::DenseMapBase<objc::DenseMap<objc_object*, unsigned long, true, objc::DenseMapInfo<objc_object*> >, objc_object*, unsigned long, objc::DenseMapInfo<objc_object*>, true>::find(objc_object* const&) + 56
0x9104d836: movl %ecx, (%edx)
0x9104d838: movl %ecx, 4(%edx)
0x9104d83b: addl $20, %esp
0x9104d83e: popl %esi
0x9104d83f: popl %ebp
0x9104d840: ret $4
0x9104d843: nop
I tried using a pre-created queue, this makes no difference. Apparently, with dependencies, this code is a problem with XCode 5.0, 32-bit.
Edit: In fact, I can isolate the problem much further. An empty Cocoa Application project in XCOde 5.0 on 10.9 with ARC on and a single method will crash. If it does not on your computer,
increase 4269 to anything bigger:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSOperationQueue* aQueue = [[ NSOperationQueue alloc ] init ];
NSMutableArray* array = [ NSMutableArray array ];
for ( int i = 0; i < 4269; i++)
[ array addObject: [ [NSOperation alloc ] init ]];
for ( int i = 1; i < [ array count ]; i++)
[[ array objectAtIndex:i ] addDependency:[array objectAtIndex:i-1]];
[ aQueue addOperations: array waitUntilFinished:NO ];
}
Edit 2:
Please note that the following code will crash in a clean new project (with ARC ON, but I think this is irrelevant). I expect the queue to keep a strong reference (retain) the NSOperation.
the NSOperationQueue is kept forever, is never released.
there is no temporary NSMutableArray.
The operations start running as soon as they are added
Here is the code:
static NSOperationQueue* aQueue = nil;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
aQueue = [[ NSOperationQueue alloc ] init ];
NSOperation* lastOp = nil;
for ( int i = 0; i < 5000 ; i++) {
NSOperation* newOp = [ NSBlockOperation blockOperationWithBlock:^{ ;} ];
if ( lastOp != nil )
[ newOp addDependency: lastOp ];
[ aQueue addOperation:newOp ];
lastOp = newOp;
}
}
New Theory
Okay, here's another theory as to what's going on:
1) Each NSOperation object keeps a strong pointer to all dependencies, probably using an array.
2) NSOperation releases the reference in dealloc, not when the dependent operation is complete.
3) As a result, you get a chain of references that prevents anything from being deallocated until the last operation completes, which does not have anything referencing it.
4) Since the queue and array are auto released, their references to the NSOperation objects go away.
5) When the last operation completes, it unleashes a string of release, dealloc, array iteration calls that, and I'm speculating here, cause a stack overflow.
I think the solution might be to keep references to the NSOperation objects manually and deliberately release them in reverse order (last to first) after they're all completed. Not really the most elegant solution, but if my theory is right, it should work.
Building on the analysis made by Daniel and Godel9, one may solve the issue by cleaning up the NSOperation dependencies while it executes:
#implementation MyOperation
- (void) main
{
...
NSArray* dependencies = self.dependencies;
for (NSOperation* op in dependencies)
[self removeDependency:op];
}
#end
Indeed, when an operation executes, all operations in its dependencies array are finished. It is not required to retain a pointer on them anymore.
It appears that Godel9 is right: the chain of references in the dependencies bring a cascade of dealloc's when the last operation completes (which I knew but did not know could cause a problem). With a sufficiently large number of objects in that chain, there is a crash. To confirm that, I wrote a program with a custom object (Holder) that retains another object that retains another etc... and I autorelease all objects. In terms of release/retain count, this is perfectly valid: all objects except the first one have a retain count higher than 1. On the next autorelease cycle, the first object is autoreleased and is the only one to not be referenced by anyone. Its release triggers a cascade of dealloc's. With a sufficiently large number of objects, this eventually crashes with a stack trace similar to my original question. I was very surprised that this crashed with a BAD_EXEC fault. I expected something along the lines of STACK_OVERFLOW (if that exists, I don't know).
This is quite obvious in hindsight (hindsight is 20/20 they say). However, I thank godel9 for pointing the issue in the context of dependencies in NSOperationQueue.
Also, the solution proposed by Godel9 (keep an array with references to all objects) will work if the object are released from the end since the very last one is not pointing to anything and will not trigger a cascade of dealloc's.
Here is the code (Foundation Tool project) I am not sure why the count needs to be so high, but the crash occurs at identical spots:
#import <Foundation/Foundation.h>
#interface Holder : NSObject
#property (strong) id object;
#property (assign) int i;
- (void) dealloc;
#end
#implementation Holder
- (void) dealloc
{
[ _object release ];
[ super dealloc ];
}
#end
int main(int argc, const char * argv[])
{
#autoreleasepool {
Holder* lastObject = nil;
for ( int i = 0; i < 1000000; i++) {
Holder* anObject = [[[ Holder alloc ] init] autorelease ];
if ( lastObject != nil )
lastObject.object = anObject;
lastObject = anObject;
}
}
}
Several years passed ... so I don't know if anyone needs the solution.
The crash is caused by stack overflow of cascade release. So one of the solutions is to break this release in time.
#implementation SafeOperation
- (void)dealloc {
__block NSArray<NSOperation *> *dependencies = nil;
#autoreleasepool {
dependencies = self.dependencies;
if ([dependencies count] == 0) {
return;
}
for (NSOperation *op in dependencies) {
[self removeDependency:op];
}
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dependencies = nil;
});
}
#end
your array is being deallocated from under you. I have no problem running your sample if the reference to the array is outside the scope of the method (all the way up to 10,000). That also explains why your code runs fine for you if you set waitUntilFinished:YES.
the reason why this doesn't always crash with lower numbers is because the memory that the array occupied has not yet been reallocated to something else.
Additionally, NSOperation is an abstract class; you shouldn't be instantiating it.
here is a modification to your code that doesn't crash (requires ARC):
static NSMutableArray* array;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSOperationQueue* aQueue = [[ NSOperationQueue alloc ] init ];
aQueue.maxConcurrentOperationCount = 1;
array = [ NSMutableArray arrayWithCapacity:10000];
#autoreleasepool {
for ( int i = 0; i < 10000; i++)
[ array addObject: [NSBlockOperation blockOperationWithBlock:(void (^)(void))^{
NSLog(#"executing %d", i);
}]];
}
for ( int i = 1; i < [ array count ]; i++)
[[ array objectAtIndex:i ] addDependency:[array objectAtIndex:i-1]];
[ aQueue addOperations: array waitUntilFinished:NO ];
}
From what I can tell you are simply trying to guarantee serial execution for your operations. why not just do aQueue.maxConcurrentOperationCount = 1;?
you remove the parent before the children and you get a crash.
Quite common with tree like structures where the leafs know (but not retain, than you'd go in circles) the parent.
don't see a bug here

UIPickerView&ActionSheet [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I try to make a PickerView with ActionSheet.The code is ok in X-code, but crash will occur when I run it and press the "Button", please help to check what have I missed?
Thank you.
Here is the .h file
#import <UIKit/UIKit.h>
#interface UIPickerView_ActionSheetViewController : UIViewController <UIActionSheetDelegate, UIPickerViewDelegate>
{
IBOutlet UIButton *button;
}
#property (nonatomic, retain) UIButton *button;
- (IBAction)buttonPressed;
#end
Here is the .m file
#import "UIPickerView_ActionSheetViewController.h"
#implementation UIPickerView_ActionSheetViewController
#synthesize button;
- (IBAction)buttonPressed
{
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"currentData"
delegate:self
cancelButtonTitle:#"Done"
destructiveButtonTitle:#"Cancel"
otherButtonTitles:nil];
UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0,40,480,200)];
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
[menu addSubview:pickerView];
[menu showInView:self.view];
[menu setBounds:CGRectMake(0,0,480, 320)];
[pickerView release];
[menu release];
}
- (void)dealloc
{
[button release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload
{
self.button=nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Crash on int retVal = UIApplicationMain(argc, argv, nil, nil);
#import <UIKit/UIKit.h>
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
Crash Info
#0 0x9631817a in __kill ()
#1 0x9631816c in kill$UNIX2003 ()
#2 0x963aa879 in raise ()
#3 0x963c09b8 in abort ()
#4 0x9619dfda in __gnu_cxx::__verbose_terminate_handler ()
#5 0x00f1c23b in _objc_terminate ()
#6 0x9619c17a in __cxxabiv1::__terminate ()
#7 0x9619c1ba in std::terminate ()
#8 0x9619c2b8 in __cxa_throw ()
#9 0x00f1c416 in objc_exception_throw ()
#10 0x00dca0bb in -[NSObject(NSObject) doesNotRecognizeSelector:] ()
#11 0x00d39966 in ___forwarding___ ()
#12 0x00d39522 in __forwarding_prep_0___ ()
#13 0x0001a4fd in -[UIApplication sendAction:to:from:forEvent:] ()
#14 0x000aa799 in -[UIControl sendAction:to:forEvent:] ()
#15 0x000acc2b in -[UIControl(Internal) _sendActionsForEvents:withEvent:] ()
#16 0x000ab7d8 in -[UIControl touchesEnded:withEvent:] ()
#17 0x0003eded in -[UIWindow _sendTouchesForEvent:] ()
#18 0x0001fc37 in -[UIApplication sendEvent:] ()
#19 0x00024f2e in _UIApplicationHandleEvent ()
#20 0x01001992 in PurpleEventCallback ()
#21 0x00da9944 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
#22 0x00d09cf7 in __CFRunLoopDoSource1 ()
#23 0x00d06f83 in __CFRunLoopRun ()
#24 0x00d06840 in CFRunLoopRunSpecific ()
#25 0x00d06761 in CFRunLoopRunInMode ()
#26 0x010001c4 in GSEventRunModal ()
#27 0x01000289 in GSEventRun ()
#28 0x00028c93 in UIApplicationMain ()
#29 0x00001c09 in main (argc=1, argv=0xbfffef8c) at main.m:14
Maybe this can help more
Attaching to process 606.
2011-12-10 06:37:08.456 UIPickerView_ActionSheet[606:207] -[UIPickerView_ActionSheetViewController DatePickerView]: unrecognized selector sent to instance 0x602dfb0
2011-12-10 06:37:08.458 UIPickerView_ActionSheet[606:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPickerView_ActionSheetViewController DatePickerView]: unrecognized selector sent to instance 0x602dfb0'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dc85a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f1c313 objc_exception_throw + 44
2 CoreFoundation 0x00dca0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d39966 ___forwarding___ + 966
4 CoreFoundation 0x00d39522 _CF_forwarding_prep_0 + 50
5 UIKit 0x0001a4fd -[UIApplication sendAction:to:from:forEvent:] + 119
6 UIKit 0x000aa799 -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x000acc2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
8 UIKit 0x000ab7d8 -[UIControl touchesEnded:withEvent:] + 458
9 UIKit 0x0003eded -[UIWindow _sendTouchesForEvent:] + 567
10 UIKit 0x0001fc37 -[UIApplication sendEvent:] + 447
11 UIKit 0x00024f2e _UIApplicationHandleEvent + 7576
12 GraphicsServices 0x01001992 PurpleEventCallback + 1550
13 CoreFoundation 0x00da9944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
14 CoreFoundation 0x00d09cf7 __CFRunLoopDoSource1 + 215
15 CoreFoundation 0x00d06f83 __CFRunLoopRun + 979
16 CoreFoundation 0x00d06840 CFRunLoopRunSpecific + 208
17 CoreFoundation 0x00d06761 CFRunLoopRunInMode + 97
18 GraphicsServices 0x010001c4 GSEventRunModal + 217
19 GraphicsServices 0x01000289 GSEventRun + 115
20 UIKit 0x00028c93 UIApplicationMain + 1160
21 UIPickerView_ActionSheet 0x00001cf9 main + 121
22 UIPickerView_ActionSheet 0x00001c75 start + 53
)
terminate called after throwing an instance of 'NSException'
Current language: auto; currently objective-c
[UIPickerView_ActionSheetViewController DatePickerView]: unrecognized selector sent to instance 0x602dfb0
What this tells you is that somewhere you are sending the message DatePickerView to your view controller instance, and it doesn't recognise it.
I can't see such a call anywhere in the code above so it is either in a different file in your project, or you have an old or renamed outlet connection in interface builder. If you have copied this code or some of the resources from somewhere else then you will need to update them to match your actual requirements.