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

  • Home
  • SEARCH
  • 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 8693577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:34:31+00:00 2026-06-13T00:34:31+00:00

I have a function: myFunction (MyProc callback, void * ref) This function is called

  • 0

I have a function:

  myFunction (MyProc callback, void * ref)

This function is called from within an Objective-C class. The function is passed a pointer to the callback (a function in the class) and a reference. The reference is necessary because the callback is called statically and therefore doesn’t have a context. The ref can be used to provide a context to the callback.

I want to be able to pass the Objective-C class as the reference. So the question is:

How do I cast an NSObject to a void * and how do I cast a void * as an NSObject.

Thanks in advance.

  • 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-13T00:34:33+00:00Added an answer on June 13, 2026 at 12:34 am

    Do something like this:

    void func(void *q)
    {
        NSObject* o = CFBridgingRelease(q);
        NSLog(@"%@", o);
    }
    
    int main(int argc, const char * argv[])
    {
        @autoreleasepool {
            NSObject* o = [NSObject new];
            func((void*)CFBridgingRetain(o));
        }
        return 0;
    }
    

    Note that CFBridgingRetain() and CFBridgingRelease() are macros around compiler attributes. Feel free to use either. I like the API variant as it is in more common use in our codebases and it is more explicitly / less confusing.

    CFBridgingRetain() effectively hard-retains the object that must be balanced by a CFBridgingRelease(). It also happens to return a CFTypeRef which is compatible with a cast to void*. CFBridgingRelease() effectively undoes that hard-retain and, thus, q will only remain valid within the scope that o is valid.

    Valid for basic callbacks, but you’d probably not what that with a void *context; type thing that has to stick around for a while. For that:

    void callback(void *context)
    {
        // grab an ARC aware reference without impacting hard-retain
        NSObject* o = (__bridge NSObject *)(context);
        NSLog(@"%@", o);
    }
    
    void freeContext(void *context)
    {
        // release the hard-retain
        CFBridgingRelease(context);
    }
    

    Note that Xcode is quite good about suggesting exactly what you should do if you leave out the cast / API call. It even explains the meanings of each of the alternative solutions (I relied on this heavily until I could keep ’em straight in my head).

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

Sidebar

Related Questions

In C++, I have a function: void MyFunction(int p) { p=5; } Assume, I
I have a function like this: MyFunction(double matrix[4][4]) {/*do stuff*/} I am calling this
Suppose I have a C function: void myFunction(..., int nObs){ int myVec[nObs] ; ...
Suppose I have the following code function myFunction(param, callback) { ... if (err) {
I have this.... function MyFunction() { var myVar = I think I am encapsulated;
I have some code like this: var content = document.getElementById('myDivId'); function MyFunction() { alert(content.style.height);
I have a function: myObject.myFunction = function(callback){ callback(); } and a callback randomObject.callBack =
I have this javascript function myFunction(source) { window.open(source, Title, 'width=400, height=400'); } and in
I have the following code function myFunction(items) { // this prints out 11 alert(items.length);
I have the following in php: public function myfunction($v){ //ini_set(memory_limit,32M); $v=mysqli_real_escape_string($this->connection,$v); $stmt=mysqli_prepare($this->connection,SELECT * from

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.