Re: Weak Linking Crash
Re: Weak Linking Crash
- Subject: Re: Weak Linking Crash
- From: Greg Parker <email@hidden>
- Date: Wed, 11 Mar 2009 18:22:36 -0700
On Mar 11, 2009, at 5:47 PM, Simone Manganelli wrote:
So I'm trying to weak link the SDL_mixer framework (svn source
avaliable here: http://svn.libsdl.org/trunk/SDL_mixer ) to a Cocoa
project, the Mac OS X port of Descent 2 (svn source available here: https://d2x-xl.svn.sourceforge.net/svnroot/d2x-xl
).
I've followed the instructions on Apple's website about weak linking
(see http://developer.apple.com/DOCUMENTATION/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html
). Specifically, I've added "-weak_framework SDL_mixer" in my
Other Linker Flags build setting.
Here's the problem: weak linking works well under *unoptimized*
builds, but fails in *optimized* builds. The program launches
correctly both with and without SDL_mixer present when unoptimized.
However, as soon as I turn on optimization, the program crashes when
the SDL_mixer framework is not present. The optimized build
continues to work fine when the SDL_mixer framework *is* present.
Since I'm shipping an optimized version, weak linking in this case
is useless, because it provides no benefit over not weak linking.
The program simply crashes if SDL_mixer is not present, no matter if
it has been weak linked or not.
The crash seems to occur *before* any code in my application; it
crashes when trying to send an appDidFinishLaunching notification.
Here is a partial stack trace:
Thread 0 Crashed:
0 ??? 0000000000 0 + 0
1 de.descent2.d2x-xl 0x0008bda3 0x1000 + 568739
2 de.descent2.d2x-xl 0x0008f7ed 0x1000 + 583661
3 de.descent2.d2x-xl 0x0008fb9c 0x1000 + 584604
4 de.descent2.d2x-xl 0x00057fc3 0x1000 + 356291
5 com.apple.Foundation 0x90b53f1c _nsnote_callback + 364
6 com.apple.CoreFoundation 0x93ea38da __CFXNotificationPost
+ 362
7 com.apple.CoreFoundation 0x93ea3bb3
_CFXNotificationPostNotification + 179
8 com.apple.Foundation 0x90b51080 -
[NSNotificationCenter postNotificationName:object:userInfo:] + 128
Those top few frames sure look like they're in your code
("de.descent2.d2x-xl"). Try running an unstripped Release build to get
a better backtrace.
It looks like it crashed because it called a NULL function pointer,
because the PC is zero. That's consistent with either "you called a
weak-linked function without checking for NULL first" or "compiler
optimized away your NULL check". The fact that an unoptimized build
works suggests the latter, but you should find the bad call and check
for sure.
-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();
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
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