On Nov 5, 2010, at 2:01 PM, Andy O'Meara wrote:
> I'm having an issue with using objc_registerThreadWithCollector() under
> earlier versions of OS X. 10.4 cancels my app load because it can't resolve
> that symbol at dynamic link time, so I'm trying to figure out how to fetch
> the symbol address by name. Of course, if the symbol isn't present then it
> won't be called. I have to be able to build and ship a binary that works
> under 10.4 so I can't just make the min OS version 10.6 at build time.
>
> Can you suggest or point me toward a snippet that demonstrates a
> Cocoa-resident symbol lookup?
It's supposed to work like this using weak symbol import:
if (objc_registerThreadWithCollector) {
objc_registerThreadWithCollector();
} else {
// do something else
}
but (1) objc_registerThreadWithCollector is missing the required availability
declaration in the current SDKs, and (2) there's a compiler "optimization" that
assumes the `if` condition is always true.
The alternative is dynamic lookup like this:
#include <dlfcn.h>
void (*registerThreadWithCollector_fn)(void);
registerThreadWithCollector_fn = (void(*)(void)) dlsym(RTLD_NEXT,
"objc_registerThreadWithCollector");
if (registerThreadWithCollector_fn) {
(*registerThreadWithCollector_fn)();
} else {
// do something else
}
The lookup can be slow, so you should do it once and save the result.
--
Greg Parker [email protected] Runtime Wrangler
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]