[cfe-users] Expected return value when making Objective C method call on nil object

Nick Tuckett nick.tuckett at marmalademail.com
Wed Jul 24 09:12:01 PDT 2013


I was wondering if there is an agreed behaviour for Clang compiled
Objective C when making a method call on a nil object? If so, does
this define exactly what should be returned in all cases, or are any
cases left as "undefined"?

With releases 3.1, 3.2 and 3.3 I've been finding some variation
between very subtly different simple cases when the method called
returns a C structure by value.

For example, given this class:

    typedef struct {
        int x;
        int y;
        int z;
    } Vec3;

    @interface Fuzz : NSObject  {
        Vec3 data;
    }
    @property Vec3 data;
    @end

    @implementation Fuzz
    @synthesize data;
    @end

This code will leave the variable "vec" uninitialised:

    Fuzz * obj = nil;
    Vec3 vec = obj.data;

Whereas this code will initialise the fields of "vec" all to zero:

    Fuzz * obj = nil;
    Vec3 vec;
    vec = obj.data;

I've studied the assembly code generated for x86 and ARM both in
unoptimised and optimised builds, and it would appear that return
value optimisation is applied regardless in the first example (even
with -O0).

This results in "vec" remaining uninitialised as though it generates
an initialised temporary on the stack, it is never copied into "vec"
nor is "vec" explicitly initialised to zero if obj is nil.

If obj is not nil, obviously the returned values go straight into vec
as one would expect with RVO.



More information about the cfe-users mailing list