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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:59:50+00:00 2026-06-12T05:59:50+00:00

I have an Objective-C method that creates a pointer, then passes it to a

  • 0

I have an Objective-C method that creates a pointer, then passes it to a new thread using performSelectorInBackground:. The problem is, I need to pass the address of the pointer to the thread because I want to free the memory associated with it from within the thread. The problem I’m having is that when the new thread is launched, the calling method ends, causing the pointer to go out of scope. How do I keep it around long enough where the thread can receive the pointer’s address?

This all works of course if I simply call the “pointerTest” method on the main thread.

Here’s my code:

-(void)pointerTest:(NSValue*)pointer
{   
    void **p = (void**)[pointer pointerValue];
    fprintf(stderr,"p was %s\n",(char*)(*p)); //prints "Hello There!"
    free(*p);
    *p = NULL;
}

Now, on the main thread:

-(IBAction)doTest:(id)sender
{
    char *str = "Hello There!";

    void *p = malloc(strlen(str)+1);
    strcpy(p, str);

    //this works
    //[self pointerTest:[NSValue valueWithPointer:&p]];

    //this fails
    [self performSelectorInBackground:@selector(pointerTest:) withObject:[NSValue valueWithPointer:&p]];

    if(!p){
        fprintf(stderr,"p was NULL!\n");
    }else{
        fprintf(stderr,"p was NOT NULL: %s\n",p);
    }
}

Update:

This code shows that if I don’t pass a pointer to pointerTest:, I cannot free p from within pointerTest:

-(void)pointerTest:(NSValue*)pointer
{   
    void *p = (void*)[pointer pointerValue];
    fprintf(stderr,"p was %s\n",(char*)p); //prints "Hello There!"
    free(p);
    fprintf(stderr,"p was %s\n",(char*)p); //STILL prints "Hello There!"
    *p = NULL;
    fprintf(stderr,"p was %s\n",(char*)p); //prints "(null)"
}

Now, in the calling method:

//I pass p, NOT a pointer to p
[self pointerTest:[NSValue valueWithPointer:p]];

if(p){
    //p is not NULL, and in fact I can print its value
    fprintf(stderr,"p was NOT NULL: %s\n",p);
}

However, if I pass a pointer to p instead of p, I can free its value as expected from within pointerTest: and upon returning from pointerTest, p is NULL in the calling method.

  • 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-12T05:59:51+00:00Added an answer on June 12, 2026 at 5:59 am

    [I won’t ask why you’re mixing malloc a C-strings with Objective-C, I guess you’re experimenting. There is nothing wrong with doing so per se, it’s just unusual in a situation like this.]

    You don’t need to pass a pointer to a pointer at all.

    A pointer is the name of a box. Some boxes contain characters, some employee records, etc. and some box names (of other boxes). malloc finds a box of the needed size and returns its name, you can pass that around just like you would pass an integer value – you don’t need to pass the name of the box containing the name of the box containing your characters…

    Also when you malloc you need to allow for the trailing NUL character on a C string, so add 1 to the string length.

    Try:

    - (void) pointerTest:(NSValue *)pointer
    {   
       void *p = (void *)[pointer pointerValue];
       fprintf(stderr,"p (%p) was %s\n", p, (char *)p);
    
       free(p);
    }
    
    - (IBAction) doTest:(id)sender
    {
       char *str = "Hello There!";
    
       void *p = malloc(strlen(str)+1); // allow for NUL
       fprintf(stderr,"p is %p\n", p);
       strcpy(p, str);
    
       // this works
       // [self pointerTest:[NSValue valueWithPointer:p]];
    
       // and so does this
       [self performSelectorInBackground:@selector(pointerTest:) withObject:[NSValue valueWithPointer:p]];
    
       if(!p)
       {
          fprintf(stderr,"p was NULL!\n");
       }
    }
    

    Note you don’t need to wrap p up in an NSValue either, nothing wrong with passing a pointer value to an Objective-C method.

    HTH

    After Comments

    You are confusing the validity of a pointer with what might happen to be in the memory the pointer refers to.

    The memory referred to by your pointer is freed. Your pointer value is no longer valid after the call to free – it is a dangling pointer.

    The memory referred to by your, now dangling, pointer value may be re-used at any time. It just happens that at the point of your fprintf the memory has not yet been re-used and so you see the old contents – freeing memory does not clear it out.

    Your store of NULL into the variable intends to remove your dangling pointer, doing so might be good practice in general but as you’ve found out taking the address of a short-lived variable and passing it to another method is a good way to create a dangling pointer. So you ended up trying to clean up a dangling pointer from box A to box B by using a dangling pointer to box A as box A had since been released itself… Let’s try that a explaining that a different way 😉

    In this case you created a pointer to the local variable (box) p belonging to doTest, as soon as doTest completes the system returns the box that was being used for p to the free-box pile (or stack in this case) and your pointer became a dangling; when your original code for pointerTest then tried to store a NULL into that box via that, now dangling, pointer you hit a problem.

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

Sidebar

Related Questions

I am newcomer in Objective C. I have an object, that creates new thread
Hi, I am new to objective C.I have created two buttons using interface builder.Instead
I am a relatively new iPhone/objective-c programmer and I have an issue that makes
Possible Duplicate: How does @synchronized lock/unlock in Objective-C? I have an application that creates
I'm relatively new to the world of Objective-C and have a class that I've
If subclass in objective-c wants to override a super class's method, does it have
This is Objective-C, in Xcode for the iPhone. I have a method in main.m:
I have an Objective-C audio app based on audio unit and my problem is
I have some Objective-C [i-os] code that I would like to run unit tests
I have some Objective-C code that looks like this: #define myVar 10 float f

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.