Crash when processing __NSAtom class object

682 views Asked by At

Hi I'm using this answer to get list of class to automatically build some structure.

My code looks like that:

NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity: 32];

Class* classes = NULL;
int numClasses = objc_getClassList(NULL, 0);
if (numClasses > 0 ) {
    classes = (Class*)malloc(sizeof(Class) * numClasses);
    numClasses = objc_getClassList(classes, numClasses);
    for (int index = 0; index < numClasses; index++) {
        Class nextClass = classes[index];

        if ([nextClass isSubclassOfClass: BaseCmd.class] &&
            ![BaseCmd isSubclassOfClass: nextClass]) {
            BaseCmd *cmd = [nextClass new];
            result[cmd.name] = cmd;
        }
    }
    free(classes);
}

return result;

So modification from answer is quite simple. It does work quite nicely until __NSAtom class is processed (index = 1548 numClasses = 7628 so it is not out of bounds problem).

When this __NSAtom class is reached there is a crash. Logs show:

*** NSForwarding: warning: object 0x7fff74978938 of class '__NSAtom' does not implement methodSignatureForSelector: -- trouble ahead
*** NSForwarding: warning: object 0x7fff74978938 of class '__NSAtom' does not implement doesNotRecognizeSelector: -- abort

Crash comes from condition isSubclassOfClass.

Why it doesn't work? How can I fix it?

I'm using OS X Yosemite.

1

There are 1 answers

1
Mats On BEST ANSWER

+[NSObject isSubclassOfClass:] is a class method for NSObject and not all classes are subclasses of NSObject.

It seems as if you have find private class that is not a subclass of NSObject, so it requires a more delicate handling for checking for inheritance. Try:

BOOL isSubclass(Class child, Class parent) {
    for (Class c = child; c != Nil; c = class_getSuperclass(c)) {
        if (c == parent) {
            return YES;
        }
   }
   return NO;
}