user2486931
user2486931

Reputation: 31

__strong qualifier used in non ARC project

The project is non-ARC enabled, however we are (mistakingly) using ARC compliant code libraries - specifically one to create singleton objects like so defined in GCDSingleton.h:

#define DEFINE_SHARED_INSTANCE
+ (id)sharedInstance
{
  static dispatch_once_t pred = 0;
  __strong static id _sharedObject = nil;
  dispatch_once(&pred, ^{
    _sharedObject = ^{return [[self alloc] init];}();
  });
  return _sharedObject;
}

This seems to work even though the shared object is defined with an __strong qualifier. I'm wondering why this doesn't cause an error or at least a warning (latest Xcode 4.6 and ios 6 sdk). Also, since the project is not ARC enabled, what exactly is that __strong qualifier doing, if anything?

Upvotes: 3

Views: 258

Answers (1)

Gabriele Petronella
Gabriele Petronella

Reputation: 108121

In MRC code, __strong is simply ignored.

I tried to compile a simple example

#import <Foundation/Foundation.h>

int main(int argc, char const *argv[]) {
    __strong NSString * foo = [[NSString alloc] initWithFormat:@"Hello, %s", argv[1]];
    NSLog(@"%@", foo);
}

with ARC

clang -fobjc-arc test.m -S -emit-llvm -o arc.ir

and without ARC

clang -fno-objc-arc test.m -S -emit-llvm -o mrc.ir

and to diff the llvm IR output.

Here's the result of diff mrc.ir arc.ir

54a55,56
>   %17 = bitcast %0** %foo to i8**
>   call void @objc_storeStrong(i8** %17, i8* null) nounwind
63a66,67
> declare void @objc_storeStrong(i8**, i8*)
> 

So basically the only difference between ARC and MRC is the addition of a objc_storeStrong call.


By the way the same code without the __strong qualifier will produce the same exact results, since __strong is the default qualifier for variables in ARC.

Upvotes: 5

Related Questions