0
votes

I create NSValue objects from SCNVector3 structs.

 NSValue *value = [NSValue valueWithSCNVector3:vector]

After that I add them to dictionary. Whenever I try to access value from dictionary app crashes, but only in release mode. Trivial stuff looks all right.

When I replace NSValue with custom object, everything works just fine. (exactly same code)


(I replaced project name and method calls with xxx)

+ (NSDictionary *)methodA:(NSArray *)nodes {

    NSMutableDictionary *nodeBounds = [NSMutableDictionary dictionary];
    for (SCNNode *node in nodes) {    
         SCNVector3 vector = [node boundingBoxMax];
         NSValue *value = [NSValue valueWithSCNVector3:vector];
         nodeBounds[node.name] = value;
    }

    NSArray *array = [self sortDictionaryKeysWithVector3:nodeBounds];

    return [nodeBounds copy];
}

 + (NSArray *)sortDictionaryKeysWithVector3:(NSDictionary *)vectors {

    NSArray *keysSortedByValue = [vectors keysSortedByValueUsingComparator:^NSComparisonResult(NSValue *vectorValue1, NSValue *vectorValue2) {

    SCNVector3 vector1 = [vectorValue1 SCNVector3Value];
    SCNVector3 vector2 = [vectorValue2 SCNVector3Value];
    return (NSComparisonResult)NSOrderedSame;
 }];

    return keysSortedByValue;
}

Stack trace:

* thread #1: tid = 0x814ff, 0x2fe336b8 libobjc.A.dylib`objc_retain + 8, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0xb39cefb2)
    frame #0: 0x2fe336b8 libobjc.A.dylib`objc_retain + 8
    frame #1: 0x000795c6 xxx`__46+[xxx sortDictionaryKeysWithVector3:]_block_invoke(.block_descriptor=<unavailable>, vectorValue1=<unavailable>, vectorValue2=<unavailable>) + 22 at xxx.m:138

frame #2: 0x21b336e6 CoreFoundation`__CFSimpleMergeSort + 290
    frame #3: 0x21b3365a CoreFoundation`__CFSimpleMergeSort + 150
    frame #4: 0x21b3365a CoreFoundation`__CFSimpleMergeSort + 150
    frame #5: 0x21b3365a CoreFoundation`__CFSimpleMergeSort + 150
    frame #6: 0x21b3365a CoreFoundation`__CFSimpleMergeSort + 150
    frame #7: 0x21b3365a CoreFoundation`__CFSimpleMergeSort + 150
    frame #8: 0x21a82e0a CoreFoundation`CFSortIndexes + 410
  * frame #9: 0x21aea2d6 CoreFoundation`-[NSDictionary keysSortedByValueWithOptions:usingComparator:] + 366
    frame #10: 0x00079528 xxx`+[xxx methodA:](self=<unavailable>, _cmd=<unavailable>, nodes=<unavailable>) + 504 at xxx:130
So putting that value in a dictionary and accessing the value causes a crash?David Rönnqvist
You should show more code and te backtraceuchuugaka
That sortDictionaryKeysWithVector3 method shouldn't even compile. The comparison block is never closed (and doesn't return the sort result) and there is no ending square bracket.David Rönnqvist
I said there is no trivial error there. It's my slip while posting. Edited.b.zdybowicz
You are still not returning a comparison result inside that block.David Rönnqvist