2 #include <objc/runtime.h>
8 static BOOL hasName(const char * const *names, const char *query)
11 while ((name = *names++)) {
12 if (strstr(name, query)) return YES;
22 unsigned int imageCount, imageCount0;
26 names = objc_copyImageNames(&imageCount0);
30 void *bundle = dlopen("unload2.out", RTLD_LAZY);
33 names = objc_copyImageNames(&imageCount);
35 testassert(imageCount == imageCount0 + 1);
36 testassert(hasName(names, "unload2.out"));
39 Class small = objc_getClass("SmallClass");
40 Class big = objc_getClass("BigClass");
44 name = class_getImageName(small);
46 testassert(strstr(name, "unload2.out"));
47 name = class_getImageName(big);
49 testassert(strstr(name, "unload2.out"));
56 // give BigClass and BigClass->isa large method caches (4692641)
57 for (i = 0; i < 10000; i++) {
58 sprintf(buf, "method_%d", i);
59 SEL sel = sel_registerName(buf);
60 objc_msgSend(o2, sel);
61 objc_msgSend(o2->isa, sel);
67 if (objc_collecting_enabled()) objc_collect(OBJC_EXHAUSTIVE_COLLECTION | OBJC_WAIT_UNTIL_DONE);
69 int err = dlclose(bundle);
71 err = dlclose(bundle);
72 testassert(err == -1); // already closed
74 testassert(!objc_getClass("SmallClass"));
75 testassert(!objc_getClass("BigClass"));
77 names = objc_copyImageNames(&imageCount);
79 testassert(imageCount == imageCount0);
80 testassert(! hasName(names, "unload2.out"));
83 // these selectors came from the bundle
84 testassert(0 == strcmp("unload2_instance_method", sel_getName(sel_registerName("unload2_instance_method"))));
85 testassert(0 == strcmp("unload2_category_method", sel_getName(sel_registerName("unload2_category_method"))));
90 // fixme object_dispose() not aggressive enough?
91 if (objc_collecting_enabled()) succeed(__FILE__);
97 // fixme heap use goes up 512 bytes after the 2nd cycle only - bad or not?
107 // 5359412 Make sure dylibs with nothing other than image_info can close
108 void *dylib = dlopen("unload3.out", RTLD_LAZY);
110 int err = dlclose(dylib);
111 testassert(err == 0);
112 err = dlclose(dylib);
113 testassert(err == -1); // already closed
115 // Make sure dylibs with real objc content cannot close
116 dylib = dlopen("unload4.out", RTLD_LAZY);
118 err = dlclose(dylib);
119 testassert(err == 0);
120 err = dlclose(dylib);
121 testassert(err == 0); // dlopen from libobjc itself
122 err = dlclose(dylib);
123 testassert(err == -1); // already closed