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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:14:27+00:00 2026-05-15T23:14:27+00:00

In my Objective-C class I alloc and release an object multiple times. I also

  • 0

In my Objective-C class I alloc and release an object multiple times. I also have to release it in dealloc. But if it has been already released previously I might get crash. Is it legal to set object to nil after releasing it? In what cases this would be wrong and cause even more problems?

  • 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-15T23:14:28+00:00Added an answer on May 15, 2026 at 11:14 pm

    Yes yes yes. You almost always should set it to nil after releasing, for exactly the reason you mentioned. You only don’t need to if you’ll never access it again (like in dealloc) or if you set it to something else immediately afterward.

    This is extra-useful because Obj-C has the feature of “nil messaging”: if you send a message to nil, it doesn’t crash, it just doesn’t do anything. So:

    MyClass *obj = [[MyClass alloc] init];
    [obj doSomething]; // this works
    [obj release]; // obj is now invalid
    [obj doSomething]; // and this would crash
    
    // but...
    MyClass *obj = [[MyClass alloc] init];
    [obj doSomething]; // this works
    [obj release]; // obj is now invalid
    obj = nil; // clear out the pointer
    [obj doSomething]; // this is a no-op, doesn't crash
    

    Another example based on your comment:

    // we have an object
    MyObject *obj = [[MyObject alloc] init];
    
    // some other object retains a reference:
    MyObject *ref1 = [obj retain];
    
    // so does another:
    MyObject *ref2 = [obj retain];
    
    // we don't need the original reference anymore:
    [obj release];
    obj = nil;
    
    [ref1 doSomething]; // this works
    // now we're done with it
    [ref1 release];
    ref1 = nil;
    
    // but someone else might still want it:
    [ref2 doSomething]; // this works too!
    [ref2 release];
    ref2 = nil; // all cleaned up!
    

    Read the Memory Management guidelines!

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

Sidebar

Related Questions

Is there a way in objective-c/Cocoa to alloc an object when the class name
I have been learning Objective C on my own for some time already and
I am writing an Objective-C class but it uses an API written in C.
I have the following objective C class. It is to store information on a
I have a peculiar problem. I created a normal Objective C class called SampleTable.
Possible Duplicate: Objective C alloc/release error When I try the code below it crashes
I've been programming objective-C for a few months now and have done pretty well
I have seen a lot of talk about dynamic typing in objective-c. But i
I've written an Objective-C class and I'm using a shared instance of it across
I am using an Objective c class, a subclass of NSObject. This class cannot

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.