Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8345031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:33:30+00:00 2026-06-09T06:33:30+00:00

I am trying to create a structure that has several different variable of different

  • 0

I am trying to create a structure that has several different variable of different types in it.

several of the types are of NSString, but trying to do this was causing an error

ARC forbids Objective-C objects in structs or unions

so having read about the error I see its sensible to add

__unsafe_unretained

before the NSString declaration, However I have no idea what the ramifications of this will be, I have had a quick read around and found this detailed post about the differences of

  • __strong
  • __weak
  • __unsafe_unretained

however it was still abit vague about whats going on with a NSString thats in a struct with __unsafe_unretained infront of it and was hoping someone can tell me whats going on and what I need to think about in the future regarding memory and stopping any leaks.

any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:33:32+00:00Added an answer on June 9, 2026 at 6:33 am

    Suppose, under ARC, you could declare a struct like this:

    typedef struct {
        __strong NSObject *someObject;
        int someInteger;
    } MyStruct;
    

    Then you might write code like this:

    MyStruct *thing = malloc(sizeof(MyStruct));
    

    Problem: malloc doesn’t zero-fill the memory it returns. So thing->someObject is some random value – not necessarily NULL. Then you assign a value to it like this:

    thing->someObject = [[NSObject alloc] init];
    

    Under the covers, ARC will turn that into code like this:

    NSObject *temporary = [[NSObject alloc] init];
    [thing->someObject release];
    thing->someObject = temporary;
    

    The problem here is that your program just sent release to some random value! Your app will probably crash at this point.

    You might say that ARC should recognize the call to malloc and take care of setting someObject to NULL to prevent this. The problem is that malloc might be wrapped in some other function, like this:

    void *myAllocate(size_t size) {
        void *p = malloc(size);
        if (!p) {
            // malloc failed.  Try to free up some memory.
            clearCaches();
            p = malloc(size);
        }
        return p;
    }
    

    OK, now ARC has to know about your myAllocate function too… and that might be inside some static library that you got as a binary.

    Your app might even have its own memory allocators that recycle old allocations without using free and malloc every time. So even changing malloc to zero-fill the memory before returning it would not work. ARC would have to know about any custom allocators in your program.

    It would be very, very hard to make this work reliably. So instead, the creators of ARC just gave up and said “Forget it. We’re not going to let you put __strong and __weak references in structs.”

    That’s why you can only put an object pointer into a struct if you use __unsafe_unretained to tell ARC “Don’t try to manage ownership of the object that this references.”

    You can try to use a struct containing __unsafe_unretained object references, perhaps using CFRetain and CFRelease to manually retain and release them. Then you can create an array of such structs. This is error-prone, so you should only do it if the profiler tells you that it’s critical for performance.

    Instead, just create a new Objective-C class instead of a struct. Give the class an @property for each field you would have put in the struct. The use an NSMutableArray to manage an array of instances of this new class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting this error while trying to create my app structure with story
I'm trying to build a modified role system that has the following class/relationship structure:
I need create a tree structure recursively. In the tree each node has different
I am trying to create a nested JSON that has data from various db
I'm trying to create a block which represents an order and that block has
I'm new to database structure. I'm trying to create an app that allows users
I am trying to create a wizard like structure using dialog boxes...So I replaced
I'm trying to create a nested storyboard structure to help break up a huge
We are trying to create/query information from a CF based on the following structure
I'm trying to working with the Uploader Plugin to create a structure where a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.