1

I am getting a crash in my class _PRLog.m in the method below -

@property (nonatomic, strong) NSNumber* logID;

- (int16_t)logIDValue
{
  NSNumber *result = [self logID];
  return [result shortValue];  // ---> ON THIS LINE
}

logID is being fetched from the Core Data which is saving attribute of type Integer-16.

I have the crashlog stated below -

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Triggered by Thread:  0

Last Exception Backtrace:

0   CoreFoundation      0x18db28300 __exceptionPreprocess + 228 (NSException.m:199)    
1   libobjc.A.dylib     0x18d83cc1c objc_exception_throw + 60 (objc-exception.mm:565)    
2   CoreFoundation      0x18da26a90 -[NSObject(NSObject) doesNotRecognizeSelector:] + 144 (NSObject.m:144)    
3   CoreFoundation      0x18db2ca60 ___forwarding___ + 1328 (NSForwarding.m:3520)    
4   CoreFoundation      0x18db2ed60 _CF_forwarding_prep_0 + 96    
5   DreamMapper         0x102bb0978 -[_PRLog logIDValue] + 48 (_PRLog.m:47)    
6   DreamMapper         0x102c07aec -[PRDevice logForLogID:] + 188 (PRDevice.m:11)    
7   DreamMapper         0x102be7c18 __48-[PRDeviceManager createDevice:success:failure:]_block_invoke_3 + 612 (PRDeviceManager.m:113)    
8   libdispatch.dylib   0x18d7c6ec4 _dispatch_call_block_and_release + 32 (init.c:1408)    
9   libdispatch.dylib   0x18d7c833c _dispatch_client_callout + 20 (object.m:495)    
10  libdispatch.dylib   0x18d7d4600 _dispatch_main_queue_callback_4CF + 832 (inline_internal.h:2484)    
11  CoreFoundation      0x18daa36b0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16 (CFRunLoop.c:1749)    
12  CoreFoundation      0x18da9e2c8 __CFRunLoopRun + 1708 (CFRunLoop.c:3069)    
13  CoreFoundation      0x18da9d8f4 CFRunLoopRunSpecific + 480 (CFRunLoop.c:3192)    
14  GraphicsServices    0x197eb4604 GSEventRunModal + 164 (GSEvent.c:2246)    
15  UIKitCore           0x191c71358 UIApplicationMain + 1944 (UIApplication.m:4823)    
16  DreamMapper         0x102c13690 main + 88 (main.m:8)    
17  libdyld.dylib       0x18d9192dc start + 4

I am guessing based on this crash log that the code crashes only when it is unable to process the shortValue of result i.e. my saved logID. However, I tested the code by passing nil for result, and it still doesn't seem to crash. I takes 0 as the shortValue. Can someone help me figure out what could be the probable cause of this crash?

CRD
  • 52,522
  • 5
  • 70
  • 86
  • Your code crashes because `[self logID]` does not return `NSNumber`. See this `doesNotRecognizeSelector`? If `logID` returns `nil`, `nil` messaging comes into play and `[result shortValue]` returns `nil`, `0`, `0.0` or `NO` (depends on type). Please, reformat your question, it's unreadable. – zrzka Jun 03 '20 at 08:20
  • As said with ` -[NSObject(NSObject) doesNotRecognizeSelector:]`, it's not that the return value is nil, it's that `result` doesn't know the method `shortValue`. It other words, it may not be a `NSNumber` instance. Question is why, but that's up to you to investigate. How is it created, etc. – Larme Jun 03 '20 at 08:23
  • So the logID is basically being saved dynamically to Core Data and is given type Integer-16 over there. When I call [self logID] it should give me a NSNumber only because of its property type. **@property (nonatomic, strong) NSNumber* logID;** Also I tried initialising result with nil, it still doesn't crash, infact it takes 0 as its short value. – priti 1911.s Jun 03 '20 at 09:27
  • 1
    @priti1911.s no, it shouldn't give you a `NSNumber` just because of how you declared it. You wrote that the type is Integer 16. But this type has checked _Use Scalar Type_ (below _Type_, _Default Value_ in the property inspector) by default which means that the property type should be declared as `@property (nonatomic) int16_t logID`. If you uncheck _Use Scalar Type_ then the property should be declared as `@property (nullable, nonatomic, copy) NSNumber *logID`. Can you check the Core Data model and this property? Type, User Scalar Type, ... – zrzka Jun 03 '20 at 14:35
  • Here https://stackoverflow.com/questions/61128529/trouble-filtering-array-of-custom-objects-by-an-nsnumber-using-nspredicate/61210987#61210987 I've shown that declaring and being are two different things. It's not because you said that it's an NSNumber that it's a NSNumber in reality. `NSNumber *number = [@[@"Hello"] firstObject];` is "valid", and what do you think `number` is in reality ? A `NSNumber` instance or a `NSString` one? – Larme Jun 03 '20 at 16:54
  • So, thank you for all your responses, looks like the value that I am receiving is not a valid NSNumber. But it doesn't really solve my problem. I am trying to debug this and when I print the `logID` , it gives me something like `PRLog(0x827FCD20)`. I am unable to figure out what this value is. – priti 1911.s Jun 15 '20 at 12:16

0 Answers0