ARC forbids Objective-C objects in structs or unions despite marking the file -fno-objc-arc

IphoneObjective CStructIos5Automatic Ref-Counting

Iphone Problem Overview


ARC forbids Objective-C objects in structs or unions despite marking the file -fno-objc-arc? Why is this so?

I had the assumption that if you mark it -fno-objc-arc you don't have this restriction.

Iphone Solutions


Solution 1 - Iphone

If you got this message try __unsafe_unretained. It is only safe, if the objects in the struct are unretained. Example: If you use OpenFeint with ARC the Class OFBragDelegateStrings says this error in a struct.

typedef struct OFBragDelegateStrings
{
     NSString* prepopulatedText;
     NSString* originalMessage;
} OFBragDelegateStrings;

to

typedef struct OFBragDelegateStrings
{
     __unsafe_unretained NSString* prepopulatedText;
     __unsafe_unretained NSString* originalMessage;
} OFBragDelegateStrings;

Solution 2 - Iphone

Rather than using a struct, you can create an Objective-C class to manage the data instead.

Solution 3 - Iphone

That is because arc can't track objects in structs or unions (since they are at that point plain C pointers).

Even though you marked the file/class in question with -fno-objc-arc you might still pass an object controlled by arc to it as parameter, which would most likely result in a memory leak.

Solution 4 - Iphone

Looks like this now works without errors, probably after this change.

i.e., You can put normal (strong) pointers to Objective-C objects in a C struct. It is managed by ARC e.g., it is unretained when the struct is destructed. Verified with:

Apple LLVM version 10.0.0 (clang-1000.11.45.2)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZsoltView Question on Stackoverflow
Solution 1 - IphonezeiteisenView Answer on Stackoverflow
Solution 2 - IphoneJánosView Answer on Stackoverflow
Solution 3 - IphonevoidSternView Answer on Stackoverflow
Solution 4 - IphoneHiroshi IchikawaView Answer on Stackoverflow