[cfe-dev] Weak pointer support on 32-bit platforms

Hovik Melikyan hovik.melikyan at gmail.com
Thu May 8 15:17:51 PDT 2014


Hi All,

I'm trying to port Apple's libobjc to Windows using clang. I'm hoping
to have Objective C with ARC and weak pointers support on both 32 and
64-bit Windows.

Most part of the port is done, I have ARC working for 32-bit Windows
binaries. I'm now facing two problems: exceptions, which is a separate
story (you probably know what this means ;) and weak pointers.

Now, I can't find a combination of compiler flags that would enable
weak pointer support for the 32-bit Windows target. Clang refuses to
do weakptrs on OSX in 32-bit mode either, but let's say that's not
required today. Bitness itself is not an issue, because after all
weakptrs exist on 32-bit iOS systems and libobjc itself is fine with
32 bits.

So what happens when you use a weak pointer, say in a program like this:

    #include <Foundation/NSObject.h>

    NSObject __weak* weakObj;

    int main()
    {
        weakObj = nil;
        return 0;
    }

is as follows: when the target is OSX with -arch i386 the compiler
says "the current deployment target does not support automated __weak
references" pointing at the __weak keyword. However, when the target
is Windows (i686-pc-win32 or x86_64-pc-win32) the compiler silently
ignores the __weak keyword and emits a objc_storeStrong call instead
of objc_storeWeak!

Let alone that potentially this may result in colossal memory leaks in
apps. On top of that, why wouldn't clang either give an error or emit
correct calls?

Here is my command that compiles the program above but produces
incorrect runtime calls for the program above (on OSX):

clang++ -target i686-pc-win32 \
        -isystem /usr/local/i686-w64-mingw32/include
-I<path-to-Foundation-headers> \
        -DTARGET_CPU_X86 -fobjc-arc -fobjc-runtime=macosx -S wtest.mm

This setup is very fragile: you change anything in these flags and
something goes wrong. I tried both the "canonical" clang and Apple's,
same effect.

I'm not familiar with clang internals, but I'd appreciate if someone
pointed me in the right direction as to what can be done. Even
patching the compiler would be acceptable for me, because in any case
it will take time until this port of libobjc matures and becomes part
of a bigger effort of emulating Cocoa on Windows.

And finally, the GNU compiler is not an option for me because it lags
in some crucial aspects of the ObjC language. I really, really want to
get clang to compile libobjc and then the entire Foundation/Cocoa
emulation layers. Ideally, clang should be able cross-compile this
stuff on OSX.

(The reason I'm writing to cfe-dev is that I thought a runtime lib is
kind of closer to the compiler itself.)

Thanks!

--
H.M.



More information about the cfe-dev mailing list