Re: Weak Linking Crash
Re: Weak Linking Crash
- Subject: Re: Weak Linking Crash
- From: Simone Manganelli <email@hidden>
- Date: Wed, 11 Mar 2009 19:22:54 -0700
Il giorno Mar 11, 2009, alle ore 6:22 PM, Greg Parker ha scritto:
-weak_framework often doesn't work by itself. If the function
declaration isn't marked weak_import in the header file, then the
compiler may optimize out your `function != NULL` check and call the
weak-linked function anyway.
This workaround might help defeat the optimizer:
// was: if (&function != NULL) function();
void * volatile function_p = &function;
if (function_p != NULL) function();
OK, this seems to be the case. I found that the code that Apple
suggests in its Weak Linking documentation to just not work. Even
when *un*optimized, the compiler seems to always think the comparison
in the if statement of the following code is true.
int result = 0;
if (MyWeakLinkedFunction != NULL)
{
result = MyWeakLinkedFunction();
}
I have been using the following syntax:
int result = 0;
uintptr_t address = (uintptr_t)(MyWeakLinkedFunction);
if (address != 0u)
{
result = MyWeakLinkedFunction();
}
The compiler likes this when *un*optimized, but it looks like the
compiler is optimizing this out, too. I found that when using your
code, however, that I needed to explicitly cast the function as a
void* before the compiler would build without errors:
void * volatile function_p = (void *)&(MyWeakLinkedFunction);
if (function_p == NULL) {
result = MyWeakLinkedFunction();
}
Thanks for your help! That solved the problem!
-- Simone
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden