Error while getting properties types of large class in objective-C - objective-c

In Objective-c, I am trying to get properties of some Object that contains about 14 property using the following code:
-(NSDictionary*) getPropertiesOfClass:(Class) clazz
{
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(clazz, &outCount);
for(i = 0; i < outCount; i++)
{
objc_property_t _property = properties[i];
const char *propName = property_getName(_property);
if(propName)
{
const char *propType = getPropertyType(_property);
NSString *propertyName = [NSString stringWithCString:propName encoding:NSUTF8StringEncoding];
NSString *propertyType = [NSString stringWithCString:propType encoding:NSUTF8StringEncoding];
[dict setValue:propertyType forKey:propertyName];
}
}
free(properties);
return dict;
}
static const char *getPropertyType(objc_property_t property)
{
const char *attributes = property_getAttributes(property);
char buffer[1 + strlen(attributes)];
strcpy(buffer, attributes);
char *state = buffer, *attribute;
while ((attribute = strsep(&state, ",")) != NULL)
{
if (attribute[0] == 'T')
{
return (const char *)[[NSData dataWithBytes:(attribute + 3) length:strlen(attribute) - 4] bytes];
}
}
return "#";
}
But I got an exception (see below) However, When I try to use the same code to get the properties list of small objects (objects with 3 or 4 properties), the code works without any exceptions and get accurate results.
So, How to handle this error!!
Thanks.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSConcreteData initWithBytes:length:copy:freeWhenDone:bytesAreVM:]: absurd length: 4294967294, maximum size: 2147483648 bytes'
*** Call stack at first throw:
(
0 CoreFoundation 0x0101e5a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x01172313 objc_exception_throw + 44
2 CoreFoundation 0x00fd6ef8 +[NSException raise:format:arguments:] + 136
3 CoreFoundation 0x00fd6e6a +[NSException raise:format:] + 58
4 Foundation 0x00800b6b -[NSConcreteData initWithBytes:length:copy:freeWhenDone:bytesAreVM:] + 135
5 Foundation 0x00811801 -[NSData(NSData) initWithBytes:length:] + 72
6 solit 0x00033449 -[ObjectMapper(private) getPropertiesOfClass:] + 489
7 solit 0x00031fa5 -[ObjectMapper objectFromDictionary:object:] + 197
8 solit 0x000327b3 -[ObjectMapper objectFromDictionary:object:] + 2259
9 solit 0x00033913 -[NSDictionary(ObjectMapper) toObject:withTypes:] + 243
10 solit 0x00031474 -[JsonWSCaller call:types:error:] + 196
11 solit 0x0003f4c2 +[SummaryWSCaller getFastData:] + 354
12 solit 0x00006fd9 -[PartSearchResultView tableView:didSelectRowAtIndexPath:] + 233
13 UIKit 0x00110b68 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1140
14 UIKit 0x00106b05 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219
15 Foundation 0x0082079e __NSFireDelayedPerform + 441
16 CoreFoundation 0x00fff8c3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
17 CoreFoundation 0x01000e74 __CFRunLoopDoTimer + 1220
18 CoreFoundation 0x00f5d2c9 __CFRunLoopRun + 1817
19 CoreFoundation 0x00f5c840 CFRunLoopRunSpecific + 208
20 CoreFoundation 0x00f5c761 CFRunLoopRunInMode + 97
21 GraphicsServices 0x012561c4 GSEventRunModal + 217
22 GraphicsServices 0x01256289 GSEventRun + 115
23 UIKit 0x000a7c93 UIApplicationMain + 1160
24 solit 0x00002289 main + 121
25 solit 0x00002205 start + 53
)

That's not related to the size of the class nor the number of properties.
This is probably because you have a problem in your memory mgmt. When there are not too much properties, the memory used to retrieve the property name and types in the const char* variables are still there, but when you start having too much properties to loop thru, these memory zones are erased by new values, hence the crash.
This is probably related to the fact that strsep is not reentrant too, so looping and using it multiple times uses the same internal char buffer each time.
You should make your getPropertyType method directly return the NSData object instead of the const char* bytes, and use NSString's initWithData:encoding: method to build the string from it (or move this initWithData:encoding: call to getPropertyType directly and make this return an NSString object directly. This way you will avoid managing pointers to memory zones that are to be autoreleased soon.
But your problem is probably that you forgot to check the validity of the attribute buffer and especially its length before trying to access the bytes in this buffer !
Especially you are using [NSData dataWithBytes:(attribute + 3) length:strlen(attribute) - 4] without checking that attribute is at least 4 characters long!
That's why the call to dataWithBytes:length: (that internally calls initWithBytes:length:copy:freeWhenDone:bytesAreVM:) crash with a NSInvalidArgumentException, telling that you pass it an absurd length of 4294967294 (which is -2 if interpreted as signed integer)

Related

Why would iOS try to call rangeOfCharacterFromSet: on an NSIndexPath?

I'm getting the following error, which causes a SIGABRT:
2015-09-10 17:54:23.859 MyApp[1310:2027719] ERROR CRASH #(null) -[NSIndexPath rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0xc000000000000016
2015-09-10 17:54:23.879 MyApp[1310:2027719] ERROR Stack Trace: (
0 CoreFoundation 0x0000000183840248 <redacted> + 160
1 libobjc.A.dylib 0x00000001952640e4 objc_exception_throw + 60
2 CoreFoundation 0x00000001838472f4 <redacted> + 0
3 CoreFoundation 0x00000001838440a8 <redacted> + 928
4 CoreFoundation 0x000000018374696c _CF_forwarding_prep_0 + 92
5 UIKit 0x0000000188632a44 <redacted> + 104
6 UIKit 0x00000001887a0ff0 <redacted> + 76
7 UIKit 0x00000001887a10b0 <redacted> + 56
8 UIKit 0x00000001887a1194 <redacted> + 36
9 QuartzCore 0x0000000187bf0820 <redacted> + 320
10 QuartzCore 0x0000000187bf06c4 <redacted> + 32
11 QuartzCore 0x0000000187befe58 <redacted> + 276
12 QuartzCore 0x0000000187befbd8 <redacted> + 528
13 QuartzCore 0x0000000187be9300 <redacted> + 80
14 CoreFoundation 0x00000001837f7ff0 <redacted> + 32
15 CoreFoundation 0x00000001837f4f7c <redacted> + 360
16 CoreFoundation 0x00000001837f535c <redacted> + 836
17 CoreFoundation 0x0000000183720f74 CFRunLoopRunSpecific + 396
18 GraphicsServices 0x000000018d0436fc GSEventRunModal + 168
19 UIKit 0x0000000188322d94 UIApplicationMain + 1488
20 MyApp 0x00000001000437b0 main + 68
21 libdyld.dylib 0x000000019590ea08 <redacted> + 4
which looks like iOS is trying to send a message to an NSString selector, but the message was sent to an NSIndexPath. Weird!
I've since found the problem; I'd set the value of the text in a UILabel to be a pointer to an object in a CoreData object's NSNumber column (0xc000000000000016) instead of an NSString. My fix was added .intValue to the NSNumber before passing it to my enumToString: method.
typedef NS_ENUM(NSInteger, MyEnum)
{
MyEnum1 = 1,
MyEnum2,
MyEnum3
};
- (NSString*)enumToString:(MyEnum)enumValue
{
switch (enumValue) {
case MyEnum1:
return #"One";
case MyEnum2:
return #"Two";
case MyEnum3:
return #"Three";
}
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
// cdObject is a managed object in CoreData. Its enumValue column is an NSNumber.
cell.textLabel.text = [self enumToString:cdObject.enumValue];
return cell;
}
This just leaves the question: Why would this cause iOS to try to send a message to the non-existant selector -[NSIndexPath rangeOfCharacterFromSet:]?
The crash occurred just after completing a -[UITableViewDataSource tableView:cellForRowAtIndexPath:]
Ok, so the cause is most likely not some threading gone wrong. Core Data turns the NSManagedObjects into faults at some point. This might be related. Also, NSNumbers are kind of special objects: the pointer is a tagged pointer, and this tagged pointer might contain the actual numeric value. This might also be related. In any case, a message was sent to a pointer, and that pointer no longer points to an object that recognizes that message. Read here about tagged pointers: https://www.mikeash.com/pyblog/friday-qa-2012-07-27-lets-build-tagged-pointers.html

SpriteKit: NSInvalidArgumentException - Attempted to add a SKNode which already has parent

I'm creating a game in SpriteKit using Objective-C. I am trying to generate blocks for the screen to be used in the game.
I'm getting an application crash on start up:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: <SKSpriteNode> name:'Block-0.000000-440.000000' texture:['nil'] position:{0, 440} size:{64, 64} rotation:0.00'
*** First throw call stack:
(
0 CoreFoundation 0x00891df6 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x0051ba97 objc_exception_throw + 44
2 CoreFoundation 0x00891d1d +[NSException raise:format:] + 141
3 SpriteKit 0x00e8c94c -[SKNode addChild:] + 119
4 Hidden Route 0x0003b3c1 -[GameScene didMoveToView:] + 2417
5 SpriteKit 0x00e6031b -[SKScene _didMoveToView:] + 97
6 SpriteKit 0x00e7b157 -[SKView presentScene:] + 283
7 Hidden Route 0x00039e9e -[GameViewController viewDidLoad] + 526
8 UIKit 0x0115ad54 -[UIViewController loadViewIfRequired] + 771
9 UIKit 0x0115b045 -[UIViewController view] + 35
10 UIKit 0x0105f477 -[UIWindow handleStatusBarChangeFromHeight:toHeight:] + 879
11 UIKit 0x0106252d +[UIWindow _noteStatusBarHeightChanged:oldHeight:forAutolayoutRootViewsOnly:] + 273
12 UIKit 0x01006ad1 __79-[UIApplication _setStatusBarHidden:animationParameters:changeApplicationFlag:]_block_invoke + 163
13 UIKit 0x010862c6 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 494
14 UIKit 0x01086701 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:animations:completion:] + 115
15 UIKit 0x0100699e -[UIApplication _setStatusBarHidden:animationParameters:changeApplicationFlag:] + 506
16 UIKit 0x01006128 -[UIApplication _updateCurrentStatusBarViewControllerAppearance] + 286
17 UIKit 0x0105223a -[UIWindow _updateContextOrderingAndSetLayerHidden:] + 548
18 UIKit 0x0105319e -[UIWindow _setHidden:forced:] + 257
19 UIKit 0x01053473 -[UIWindow _orderFrontWithoutMakingKey] + 49
20 UIKit 0x01061615 -[UIWindow makeKeyAndVisible] + 80
21 UIKit 0x00ffecd6 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3108
22 UIKit 0x0100216d -[UIApplication _runWithMainScene:transitionContext:completion:] + 1639
23 UIKit 0x0101ad30 __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke + 59
24 UIKit 0x01000d7f -[UIApplication workspaceDidEndTransaction:] + 155
25 FrontBoardServices 0x06f909de __37-[FBSWorkspace clientEndTransaction:]_block_invoke_2 + 71
26 FrontBoardServices 0x06f9046f __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 54
27 FrontBoardServices 0x06fa2425 __31-[FBSSerialQueue performAsync:]_block_invoke + 26
28 CoreFoundation 0x007b57a0 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 16
29 CoreFoundation 0x007ab0b3 __CFRunLoopDoBlocks + 195
30 CoreFoundation 0x007aaf0b __CFRunLoopRun + 2715
31 CoreFoundation 0x007aa1ab CFRunLoopRunSpecific + 443
32 CoreFoundation 0x007a9fdb CFRunLoopRunInMode + 123
33 UIKit 0x01000744 -[UIApplication _run] + 571
34 UIKit 0x01003e16 UIApplicationMain + 1526
35 Hidden Route 0x0003c41d main + 141
36 libdyld.dylib 0x02fa0ac9 start + 1
37 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
I've looked at the stack trace and found that the method causing the issue is my didMoveToView: method, which is this:
-(void)didMoveToView:(SKView *)view
{
// ViewDidLoad
[self initialize];
/* Setup your scene here
SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:#"Arial"];
myLabel.text = #"Hello, World!";
myLabel.fontSize = 65;
myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
*/
player = [SKSpriteNode spriteNodeWithColor:[AppDelegate getRandomColor] size:CGSizeMake(40, 40)];
player.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self generateBoard];
[self generateNextAtIndex:kNeed - 3];
int width = (self.frame.size.width) / [AppDelegate numberOfColumns];
int height = (self.frame.size.height) / kNeed;
for (int i = 0; i < boardRows.count; i++)
{
Row *current = [boardRows objectAtIndex:i];
for (int j = 0; j < current.blocks.count; j++)
{
Block *block = [current.blocks objectAtIndex:j];
block.anchorPoint = CGPointMake(0, 1);
block.position = CGPointMake((j * width), self.frame.size.height - (i * width));
[block setName:[NSString stringWithFormat:#"Block-%f-%f", block.position.x, block.position.y]];
NSLog(#"Position for item %i", i);
block.size = CGSizeMake(width, width);
[self addChild:block];
}
}
[self addChild:player];
}
I have a feeling that it's something to do with the self addChild:block line however I can't figure out a solution. If you need any more information I'll post it under here (just ask for it)
Thanks for your help
EDIT 1:
generateBoard method:
- (void)generateBoard
{
int rnd = 2;
int from = rnd;
int to = 0;
Row *row = [defaultRows objectAtIndex:rnd];
[boardRows replaceObjectAtIndex:0 withObject:row];
for (int i = 2; i < boardRows.count; i++)
{
rnd = arc4random() % [AppDelegate numberOfColumns];
to = rnd;
Row *rowThis = [defaultRows objectAtIndex:rnd];
Row *rowBelow = [[Row alloc] initWithSize:CGSizeMake(self.frame.size.width, self.frame.size.height) need:kNeed];
[rowBelow generateWithPoint:from toPoint:to];
[boardRows replaceObjectAtIndex:i withObject:rowThis];
[boardRows replaceObjectAtIndex:i-1 withObject:rowBelow];
to = from;
from = rnd;
// to make it add 2
i++;
}
}
EDIT 2:
After adding in the Exception Breakpoint, it revealed that the line causing the issue is the self addChild:block line
The answer to this was really simple, basically I had some pre configured blocks to make the generation easier, however in doing this, calling self addChild:block was actually trying to add the same block (with the same memory address) into 2 places at once, and therefore was crashing the app.
I just added a duplicate function to my Block class and started to implement that rather than calling the block directly.

showing elements of dictionary

I have a plist file that contains array of dictionaries, and I'm trying to show in console the elements of the dictionnaries... here is my code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"validrep" ofType:#"plist"];
descArray = [[NSMutableArray alloc] init];
NSString *key = [NSString stringWithFormat:#"test %i",i+1];
NSMutableArray *tabreponses = [[NSMutableArray arrayWithContentsOfFile:path] retain];
NSDictionary *dictreponses = [NSDictionary dictionaryWithObject:[tabreponses objectAtIndex:i] forKey:key];
[descArray addObject:dictreponses];
NSDictionary *dictionary = [descArray objectAtIndex:i];
NSArray *array = [dictionary objectForKey:key];
NSLog(#"the array is %#, it contains %i elements.",array,[array count]);
Till now everythig is working correctly and this is what the console is showing:
2011-12-14 14:52:33.845 Daltonien[1459:b603] the array is {
rep1 = "1 \U00e9toile derri\U00e8re un quadrillage ";
rep2 = "1 lettre B derri\U00e8re un quadrillage";
rep3 = "1 quadrillage seul ";
}, it contains 3 elements.
but it doesn't work when i try to show the first element of the array, i do it like this:
NSLog(#"the array is %#, it contains %i elements, the first element is %# .",array,[array count],[array objectAtIndex:i]);
This results in an exception:
Daltonien[1478:b603] -[__NSCFDictionary objectAtIndex:]:
unrecognized selector sent to instance 0x4b8cbd0
Daltonien[1478:b603] *** Terminating app due to
uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSCFDictionary objectAtIndex:]:
unrecognized selector sent to instance 0x4b8cbd0'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dca5a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f1e313 objc_exception_throw + 44
2 CoreFoundation 0x00dcc0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d3b966 ___forwarding___ + 966
4 CoreFoundation 0x00d3b522 _CF_forwarding_prep_0 + 50
5 Daltonien 0x00002cff -[DetailViewController tableView:numberOfRowsInSection:] + 193
6 UIKit 0x004772b7 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 1834
7 UIKit 0x00474d88 -[UITableViewRowData numberOfRows] + 108
8 UIKit 0x00328677 -[UITableView noteNumberOfRowsChanged] + 132
9 UIKit 0x00335708 -[UITableView reloadData] + 773
10 UIKit 0x00332844 -[UITableView layoutSubviews] + 42
11 QuartzCore 0x01d6aa5a -[CALayer layoutSublayers] + 181
12 QuartzCore 0x01d6cddc CALayerLayoutIfNeeded + 220
13 QuartzCore 0x01d120b4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
14 QuartzCore 0x01d13294 _ZN2CA11Transaction6commitEv + 292
15 QuartzCore 0x01d1346d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
16 CoreFoundation 0x00dab89b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
17 CoreFoundation 0x00d406e7 __CFRunLoopDoObservers + 295
18 CoreFoundation 0x00d091d7 __CFRunLoopRun + 1575
19 CoreFoundation 0x00d08840 CFRunLoopRunSpecific + 208
20 CoreFoundation 0x00d08761 CFRunLoopRunInMode + 97
21 GraphicsServices 0x017211c4 GSEventRunModal + 217
22 GraphicsServices 0x01721289 GSEventRun + 115
23 UIKit 0x002c8c93 UIApplicationMain + 1160
24 Daltonien 0x000020c8 main + 102
25 Daltonien 0x00002059 start + 53
26 ??? 0x00000001 0x0 + 1
)
terminate called throwing an exception
with what should i change objectAtIndex:i
THANX for helping me.
The problem is this:
Your key, #"test1", is actually a dictionary, not an array...
To get the first key / value of the dictionary, you would do the following
[[myDictionary keys] objectAtIndex:0]
[[myDictionary values] objectAtIndex:0]
If you want the first object in your array, you'll want something like this:
[array objectAtIndex:0];
To loop through all of the objects, this will do it:
for (id someObject in array) {
// Do stuff
}

SIGABRT while adding objects to an NSMutableArray

I have a method which returns an NSArray with points to be plotted. Following is the code for my method.
- (NSArray *)pointsToPlot:(GraphView *)requester
{
NSMutableArray *points = [[NSSet alloc] init];
CGPoint midPoint;
midPoint.x = self.graph.bounds.origin.x + self.graph.bounds.size.width / 2;
midPoint.y = self.graph.bounds.origin.y + self.graph.bounds.size.height / 2;
//Find points to plot
NSValue *point1 = [NSValue valueWithCGPoint:CGPointMake(midPoint.x - 10, midPoint.y)];
NSValue *point2 = [NSValue valueWithCGPoint:CGPointMake(midPoint.x, midPoint.y - 10)];
NSValue *point3 = [NSValue valueWithCGPoint:CGPointMake(midPoint.x + 10, midPoint.y)];
[points addObject:point1];
[points addObject:point2];
[points addObject:point3];
[points autorelease];
return points;
}
I am getting SIGABRT as soon as I add point1 to the points array.
Following is the stack trace obtained:
2011-10-27 10:43:36.939 Graphing Calculator[7056:207] -[__NSSet0 addObject:]: unrecognized selector sent to instance 0x4b57900
2011-10-27 10:43:36.943 Graphing Calculator[7056:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSet0 addObject:]: unrecognized selector sent to instance 0x4b57900'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dc95a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f1d313 objc_exception_throw + 44
2 CoreFoundation 0x00dcb0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d3a966 ___forwarding___ + 966
4 CoreFoundation 0x00d3a522 _CF_forwarding_prep_0 + 50
5 Graphing Calculator 0x000073cd -[GraphViewController pointsToPlot:] + 1037
6 Graphing Calculator 0x00006895 -[GraphView drawRect:] + 965
7 UIKit 0x00053187 -[UIView(CALayerDelegate) drawLayer:inContext:] + 426
8 QuartzCore 0x016b3b5e -[CALayer drawInContext:] + 143
9 QuartzCore 0x016bfe47 _ZL16backing_callbackP9CGContextPv + 85
10 QuartzCore 0x0160d1f7 CABackingStoreUpdate + 2246
11 QuartzCore 0x016bfd24 -[CALayer _display] + 1085
12 QuartzCore 0x016b627d CALayerDisplayIfNeeded + 231
13 QuartzCore 0x0165b0c3 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 325
14 QuartzCore 0x0165c294 _ZN2CA11Transaction6commitEv + 292
15 QuartzCore 0x0165c46d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
16 CoreFoundation 0x00daa89b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
17 CoreFoundation 0x00d3f6e7 __CFRunLoopDoObservers + 295
18 CoreFoundation 0x00d081d7 __CFRunLoopRun + 1575
19 CoreFoundation 0x00d07840 CFRunLoopRunSpecific + 208
20 CoreFoundation 0x00d07761 CFRunLoopRunInMode + 97
21 GraphicsServices 0x010011c4 GSEventRunModal + 217
22 GraphicsServices 0x01001289 GSEventRun + 115
23 UIKit 0x00029c93 UIApplicationMain + 1160
24 Graphing Calculator 0x000021d9 main + 121
25 Graphing Calculator 0x00002155 start + 53
)
terminate called after throwing an instance of 'NSException'
Could someone please explain me what am I doing wrong here?
Change
NSMutableArray *points = [[NSSet alloc] init];
to
NSMutableArray *points = [[NSMutableArray alloc] init];
You are creating and initializing an NSSet and storing it to a reference for NSMutableArray.
Change the following line:
NSMutableArray *points = [[NSSet alloc] init];
To this line:
NSMutableArray *points = [[NSMutableArray alloc] initWithCapacity:3];

Variable is Not A CFString Error

Hey fellas, while running through a debugger I am seeing the following appear the second time it sets the variables (timestamp and checksum are set through this method one after the other, it works fine when no DataFeedManager exists, but upon returning to it again it crashes when it's time to set the checksum):
Here is the function of interest:
//sets specified attribute to the passed in value while ensuring that only one instance of the DataFeedManager exists
-(void)setItemInDFMWhilePreservingEntityUniquenessForItem:(attribute)attr withValue:(id)value {
SJLog(#"CoreDataSingleton.m setItemInDFMWhilePreservingEntityUniquenessForItem");
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"DataFeedManager" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSUInteger numEntities = [[self managedObjectContext] countForFetchRequest:fetchRequest error:&error];
if (numEntities == NSNotFound) { // ERROR
//...
} else if (numEntities == 0) {
DataFeedManager *dfm = (DataFeedManager *)[NSEntityDescription insertNewObjectForEntityForName:#"DataFeedManager"
inManagedObjectContext:[self managedObjectContext]];
if (attr == checksumAttr) { //BLOCK OF INTEREST
NSString *tempVal = [[NSString alloc] initWithString:value];
[dfm setLastUpdateCheckSum:[NSString stringWithString:tempVal]];
} else if (attr == timeStampAttr) {
[dfm setTimeStamp:value];
}
} else { // more than zero entities
if (numEntities == 1) {
NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
if (attr == checksumAttr) { //BLOCK OF INTEREST
NSString *tempVal = [[NSString alloc] initWithString:value];
[[fetchedObjects objectAtIndex:0] setLastUpdateCheckSum:[NSString stringWithString:tempVal]]; //crashes at this line, after successfully going through the previous BLOCK OF INTEREST area
} else if (attr == timeStampAttr) {
[[fetchedObjects objectAtIndex:0] setTimeStamp:value];
}
} else { // ERROR: more than one entity
//...
}
} // else more than zero entities
[fetchRequest release];
}//setItemInDFMWhilePreservingEntityUniquenessForItem:withValue:
I have marked the areas of interest with //BLOCK OF INTEREST comments and have indicated upon which line the crash occurs (scroll right to see it!). Here is a readout of error from the console:
2011-04-22 17:18:10.924 Parking[26783:207] CoreDataSingleton.m setItemInDFMWhilePreservingEntityUniquenessForItem
2011-04-22 17:18:10.924 Parking[26783:207] -[__NSCFDictionary length]: unrecognized selector sent to instance 0xac34850
2011-04-22 17:18:10.970 Parking[26783:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary length]: unrecognized selector sent to instance 0xac34850'
*** Call stack at first throw:
(
0 CoreFoundation 0x011a0be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x012f55c2 objc_exception_throw + 47
2 CoreFoundation 0x011a26fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x01112366 ___forwarding___ + 966
4 CoreFoundation 0x01111f22 _CF_forwarding_prep_0 + 50
5 Foundation 0x00c4d1e1 -[NSPlaceholderString initWithString:] + 162
6 Foundation 0x00c562c2 +[NSString stringWithString:] + 72
7 Parking 0x0000e4d4 -[CoreDataSingleton setItemInDFMWhilePreservingEntityUniquenessForItem:withValue:] + 774
8 Parking 0x00008bb4 -[DataUpdater allDataRetrievedWithSuccess:withError:] + 225
9 Parking 0x0000952e -[DataUpdater dataDownloadCompleted:forFunc:withData:withError:] + 769
10 Parking 0x00010bb5 -[DataRetriever finish] + 432
11 Parking 0x00010e75 -[DataRetriever connectionDidFinishLoading:] + 36
12 Foundation 0x00c61172 -[NSURLConnection(NSURLConnectionReallyInternal) sendDidFinishLoading] + 108
13 Foundation 0x00c610cb _NSURLConnectionDidFinishLoading + 133
14 CFNetwork 0x0348e606 _ZN19URLConnectionClient23_clientDidFinishLoadingEPNS_26ClientConnectionEventQueueE + 220
15 CFNetwork 0x03559821 _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 293
16 CFNetwork 0x03559b0f _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 1043
17 CFNetwork 0x03484e3c _ZN19URLConnectionClient13processEventsEv + 100
18 CFNetwork 0x03484cb7 _ZN17MultiplexerSource7performEv + 251
19 CoreFoundation 0x0118201f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
20 CoreFoundation 0x010e019d __CFRunLoopDoSources0 + 333
21 CoreFoundation 0x010df786 __CFRunLoopRun + 470
22 CoreFoundation 0x010df240 CFRunLoopRunSpecific + 208
23 CoreFoundation 0x010df161 CFRunLoopRunInMode + 97
24 GraphicsServices 0x01414268 GSEventRunModal + 217
25 GraphicsServices 0x0141432d GSEventRun + 115
26 UIKit 0x0004e42e UIApplicationMain + 1160
27 Parking 0x00002698 main + 102
28 Parking 0x00002629 start + 53
)
terminate called after throwing an instance of 'NSException'
I believe it has something to do with copying the string adequately (can't set a string I don't own to the store). I have tried placing [value copy] as well as &value(saw this sort of thing work for someone else, so I thought I would give it a shot) to no avail. Shouldn't my current method adequately take ownership of the string? I still can't figure out what I am doing wrong. Any help appreciated. Thanks!
Best guess (based in part on this answer) is that you're passing in a released object as the value when you call the method the second time, or possibly that value is of class NSDictionary on your second time through – it's not clear from this code snippet why your method takes an argument of type id and then blithely treats it as an instance of NSString, but this may be part of the problem.
Note that your -setItemInDFMWhilePreservingEntityUniquenessForItem:withValue: accepts an arbitrary object in its second argument (type id).
Inside the method, you do:
NSString *tempVal = [[NSString alloc] initWithString:value];
Unless value is an Objective-C string, this will crash your program. In fact, your crash log shows that in that particular execution value was an NSDictionary. You need to make sure that value is an NSString.
Also, note that you own the string you’re assigning to tempVal since you’ve used +alloc. Don’t forget to release that string.