Skip to content

Instantly share code, notes, and snippets.

@mikeash
Created October 6, 2011 14:52
Show Gist options
  • Save mikeash/1267596 to your computer and use it in GitHub Desktop.
Save mikeash/1267596 to your computer and use it in GitHub Desktop.
ARC and objc_getClassList do not get along
// clang -framework Foundation -fobjc-arc test.m
#import <objc/runtime.h>
#import <stdio.h>
#import <stdlib.h>
int main(int argc, char **argv)
{
@autoreleasepool
{
int count = objc_getClassList(NULL, 0);
Class buffer[count];
objc_getClassList(buffer, count);
for(int i = 0; i < count; i++)
{
Class c = buffer[i];
// see if c is in the NSObject hierarchy
BOOL isNSObj = NO;
// this will crash here when trying to retain a class which shouldn't be messaged
for(Class candidate = c; candidate != Nil; candidate = class_getSuperclass(candidate))
{
if(candidate == objc_getClass("NSObject"))
isNSObj = YES;
}
if(!isNSObj)
fprintf(stderr, "%s is not in the NSObject hierarchy\n", class_getName(c));
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment