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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:30:03+00:00 2026-05-27T22:30:03+00:00

I have a simple objective c object NSManagedObjectContext * moc = nil Now I

  • 0

I have a simple objective c object

NSManagedObjectContext * moc = nil

Now I need to pass it into a function in an ARC environment that accepts parameter of type

void *volatile * value

I tried

&((__bridge void *)moc))

but I get the following compiler error

Address expression must be lvalue or a function pointer

I also tried

void ** addr = (__bridge void **)(&moc);

But I get the error

Incompatible types casting 'NSManagedObjectContext * __strong *' to 'void **' with a __bridge cast

Is there any way to first cast and then get the address of the casted pointer?

EDIT

To be more specific, I’m trying to implement the singleton pattern described in another stackoverflow question, but I’m having trouble feeding the address of the objective c NSManagedObjectContext object into the third argument of the following function in an ARC environment.

OSAtomicCompareAndSwapPtrBarrier(void *__oldValue, void *__newValue, void *volatile *__theValue)

  • 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-05-27T22:30:03+00:00Added an answer on May 27, 2026 at 10:30 pm

    Another modern singleton pattern I’ve been seeing/using a lot in the modern era uses GCD’s dispatch_once. You asked directly about pointer casting for ARC (and more on that below), but in terms of filling your actual need, why not just do this with GCD? Like this:

    + (NSFoo*)sharedFoo
    {
        static dispatch_once_t pred;
        static NSFoo* sFoo;
        dispatch_once(&pred, ^{ sFoo = [[NSFoo alloc] init]; } );
        return sFoo;
    }
    

    As you can see if you go look at the source for dispatch_once, GCD implements the same pattern you’re seeking to implement here. GCD gets around the problem you’re having by not using an ARC-tracked pointer as the thing it CompareAndSwaps, but rather using a surrogate value (here pred), so even if you have some aversion to using GCD for your singleton, you might consider mimicking their approach and using a surrogate.

    That said, if you just use GCD for your case, you don’t ever have to worry about stuff like gnarly ARC pointer casting. You can also be reasonably sure that the GCD implementation will behave consistently across multiple platforms/architectures (MacOS/iOS).

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

Sidebar

Related Questions

I have an object that I've written in objective c. It is very simple
I have simple win service, that executes few tasks periodically. How should I pass
Is it possible to create a simple objective C object in xcode that has
I have simple SSIS package which reads data from flat file and insert into
I have simple SSIS package where I import data from flat file into SQL
I have simple WinForms application where modifying Windows Registry. The problem is that in
I have a simple question about creating multiple initialisers within an objective-c class. Basically
I'm new to Objective-C and I have been working with simple programs, and I
I'm developing simple MVC app in Cocoa/Objective-C. I have a strange issue (or misunderstanding)
When Connected: I have a simple HTML page with some image elements that reference

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.