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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:46:57+00:00 2026-06-13T07:46:57+00:00

Problem : I have an __unsafe_unretained id pointer that points to an already released

  • 0

Problem:

I have an __unsafe_unretained id pointer that points to an already released object. So far so good, as long as I do not “use” the pointer at all (in particular, I do not call any method through the pointer). However, when I try to return its value from a method, it crashes, even if I have explicitly specified that the return value has the type __unsafe_unretained id. Why is that? I thought if I use __unsafe_unretained, it would not call methods like retain / release / autorelease at all? I thought I can use __unsafe_unretained id pretty much as if it is a void* (meaning that it only does simple, native assignments)?

Environment:

  • Developing on Xcode 4.4.1
  • Using iOS SDK 5.1
  • ARC is enabled
  • Running on iPhone 4.3 / 5.0 / 5.1 Simulator or iPhone 4.3 Device
  • Crashes on both Debug and Release builds

Source Code:

// Declare my class with 1 member.
@interface MyClass : NSObject
{
    __unsafe_unretained id      m_MyMember;
}
@end

// **************************************************************************************************** //

// Implement my class.
@implementation MyClass

// Setter
-(void)SetMember:(__unsafe_unretained id)member
{
    m_MyMember = member;
}

// Getter: by passing parameter by reference
-(void)GetMember1:(__unsafe_unretained id*)member
{
    *member = m_MyMember;   // No problem.
}

// Getter: by return value
-(__unsafe_unretained id)GetMember2
{
    return m_MyMember;  // Crashed in here!
}

@end

// **************************************************************************************************** //

//! Application entry point.
int main(int argc, char *argv[])
{
    @autoreleasepool
    {       
        {
            // Create an object that dies immediately. deadObj is a dangling pointer.
            __unsafe_unretained id deadObj = [[NSMutableString alloc] initWithFormat:@"%d", 12];

            // Create my object.
            MyClass* myObject = [[MyClass alloc] init];

            // Assign my member.
            [myObject SetMember:deadObj];

            // Get back my member: by passing parameter by reference
            __unsafe_unretained id unsafePointer1;
            [myObject GetMember1:&unsafePointer1];  // No problem.

            // Get back my member: by return value
            __unsafe_unretained id unsafePointer2;
            unsafePointer2 = [myObject GetMember2]; // Crashed in here!

            int BreakpointHere = 0;
        }
    }
}

Call Stack (iPhone 4.3 Simulator/iOS 4.3 Device):

#0  0x011db09b in objc_msgSend ()
#1  0x00106712 in __arclite_objc_retainAutoreleaseReturnValue at /SourceCache/arclite_host/arclite-29.1/source/arclite.m:259
#2  0x00001fec in -[MyClass GetMember2] at /Users/user/SourceCode/main.m:28
#3  0x00002147 in main at /Users/user/SourceCode/main.m:56

Call Stack (iPhone 5.0/5.1 Simulator):

#0  0x014f6d25 in objc_retain ()
#1  0x014f7fe3 in objc_retainAutoreleaseReturnValue ()
#2  0x00001fec in -[MyClass GetMember2] at /Users/user/SourceCode/main.m:28
#3  0x00002147 in main at /Users/user/SourceCode/main.m:56
  • 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-13T07:46:59+00:00Added an answer on June 13, 2026 at 7:46 am

    I think the behavior can be explained with the following information 3.2.3. Unretained return values in the Automatic Reference Counting documentation:

    A method or function which returns a retainable object type but does
    not return a retained value must ensure that the object is still valid
    across the return boundary.

    When returning from such a function or method, ARC retains the value
    at the point of evaluation of the return statement, then leaves all
    local scopes, and then balances out the retain while ensuring that the
    value lives across the call boundary. In the worst case, this may
    involve an autorelease, but callers must not assume that the value is
    actually in the autorelease pool.

    Your function GetMember2 returns an id, which is a retainable object type. Therefore the ARC compiler adds retain/autorelease calls to make sure that the returned object is still valid when the function returns. This crashes because m_MyMember does not point to a valid object.

    Declaring the return type as (__unsafe_unretained id) does not change this behavior, in fact I assume that the __unsafe_unretained is ignored here.

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

Sidebar

Related Questions

I've seen that a few instances of this problem have been raised already. However,
Background: I have a project that is not at all large scale (2 ASP.NET
Problem: I have a table that prints out vertical but I would like it
Problem: I have a value that is set in $record, for instance 1.69. Then
Problem: I have a string that looks like this: [1=>2,3,4][5=>6,7,8][9=>10,11,12][13=>14,15][16=>17,18] Question: How can you
Problem I have computed a probability density function that depends on two variables. I
I'm running into a problem that I'm not sure how to solve. Let me
Possible Duplicate: Generics in for each loop problem if instance does not have generic
I have an interesting design problem and I was hoping you all could make
i have some conditions to pass to a finder. The problem is that i

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.