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 8040369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:52:26+00:00 2026-06-05T03:52:26+00:00

I tried making a struct with classes in it like: struct my_struct { NSString

  • 0

I tried making a struct with classes in it like:

struct my_struct
{
    NSString *string;
    // more fields
};

To my surprise, Objective-C++ allowed this with ARC enabled.
How will it manage the string?
It can easily retain in each assignment but release is the problem.
It can add a destructor with release in it, but this will make the struct non-trivial.
It can also make this not retain or release, but to do so there should be unsafe_unretained.

From my observation nothing crashes when using this, but I would like to know what really happens here.

  • 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-05T03:52:27+00:00Added an answer on June 5, 2026 at 3:52 am

    See 4.3.5 of the ARC docs:

    4.3.5. Ownership-qualified fields of structs and unions

    A program is ill-formed if it declares a member of a C struct or union
    to have a nontrivially ownership-qualified type.

    Rationale: the resulting type would be non-POD in the C++ sense, but C
    does not give us very good language tools for managing the lifetime of
    aggregates, so it is more convenient to simply forbid them. It is
    still possible to manage this with a void* or an __unsafe_unretained
    object.

    This restriction does not apply in Objective-C++. However, nontrivally
    ownership-qualified types are considered non-POD: in C++11 terms, they
    are not trivially default constructible, copy constructible, move
    constructible, copy assignable, move assignable, or destructible. It
    is a violation of C++’s One Definition Rule to use a class outside of
    ARC that, under ARC, would have a nontrivially ownership-qualified
    member.

    Rationale: unlike in C, we can express all the necessary ARC semantics
    for ownership-qualified subobjects as suboperations of the (default)
    special member functions for the class. These functions then become
    non-trivial. This has the non-obvious result that the class will have
    a non-trivial copy constructor and non-trivial destructor; if this
    would not normally be true outside of ARC, objects of the type will be
    passed and returned in an ABI-incompatible manner.

    If you read through all the caveats, I would strongly recommend against doing this in ObjC++. I strongly recommend against extensive use of ObjC++ in any case. It is a bridging language to help pure ObjC and pure C++ talk to each other. It has many problems. Combining ObjC++ with ARC introduces both time and space performance costs that do not occur in ObjC in order to make it exception-safe. Defining these kinds of ObjC++-specific data structures makes it hard to interact with non-ObjC++ code and non-ARC code (note the caveat that you cannot use this outside of ARC). Much of what you should get for free from ARC suddenly becomes hard as you have to worry again about memory management (as you’ve already discovered).

    Build a pure-ObjC layer. Build a pure C++ layer. Build a thin ObjC++ layer to tie the two together. Do not put ObjC objects in structs, and definitely not in any public structs (i.e. visible outside of the single ObjC++ object that defines it).

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

Sidebar

Related Questions

I tried making a custom configuration class that went on like this: WatcherServiceInfoSection<TWatcherServiceDetailElement> where
I tried making a wrapper class that encapsulates an object, a string (for naming
Has anyone managed to do this? I tried making a managed wrapper class for
I've tried making my hole tr tag to be clickable so I made this
Using J2SE, I tried making a LAN application using java.net.*, and it worked perfectly.
I'm making the script for easy upload many files. I've tried fyneworks plugin ,
Tried this: $('.link').click(function(e) { $.getScript('http://www.google.com/uds/api?file=uds.js&amp;v=1.0', function() { $('body').append('<p>GOOGLE API (UDS) is loaded</p>'); }); return
I'm making a vector/matrix library. (GCC, ARM NEON, iPhone) typedef struct{ float v[4]; }
I'm making a vector drawing application. I use this algorithm to generate outlines. This
i tried making library with ar -r -c -s libtestlib.a *.o as given in

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.