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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:46:15+00:00 2026-06-04T23:46:15+00:00

In objective c, suppose I have an object Obj stored in a NSMutableArray, and

  • 0

In objective c, suppose I have an object Obj stored in a NSMutableArray, and the array’s pointer to it is the only strong pointer to Obj in the entire program. Now suppose I call a method on Obj and I run this method in another thread. In this method, if Obj sets the pointer for itself equal to nil will it essentially delete itself? (Because there will be no more strong pointers left) I suspect the answer is no, but why? If this does work, is it bad coding practice (I assume its not good coding, but is it actually bad?)

  • 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-04T23:46:17+00:00Added an answer on June 4, 2026 at 11:46 pm

    It is highly unlikely that an object would be in a position to cause its own release/deallocation if your code is designed properly. So yes, the situation you describe is indicative of bad coding practice, and can in fact cause the program to crash. Here is an example:

    @interface Widget : NSObject
    @property (retain) NSMutableArray *array;
    @end
    
    @implementation Widget
    
    @synthesize array;
    
    - (id)init
    {
        self = [super init];
        if(self) {
            array = [[NSMutableArray alloc] init];
            [array addObject:self];
        }
        return self;
    }
    
    - (void)dealloc
    {
        NSLog(@"Deallocating!");
        [array release];
        [super dealloc];
    }
    
    - (void)removeSelf
    {
        NSLog(@"%d", [array count]);
        [array removeObject:self];
        NSLog(@"%d", [array count]);
    }
    @end
    

    and then this code is in another class:

    Widget *myWidget = [[Widget alloc] init];
    [myWidget release];  // WHOOPS!
    [myWidget removeSelf];
    

    The second call to NSLog in removeSelf will cause an EXC_BAD_ACCESS due to the fact that array has been deallocated at that point and can’t have methods called on it.

    There are at least a couple mistakes here. The one that ultimately causes the crash is the fact that whatever class is creating and using the myWidget object releases it before it is finished using it (to call removeSelf). Without this mistake, the code would run fine. However, MyWidget shouldn’t have an instance variable that creates a strong reference to itself in the first place, as this creates a retain cycle. If someone tried to release myWidget without first calling removeSelf, nothing would be deallocated and you’d probably have a memory leak.

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

Sidebar

Related Questions

Suppose I have a Contact object with a name and photo only @interface Contact:
Working in Objective C here. Suppose I have an array named fooArray . Each
Suppose I have an object like following: var Obj = { a: { name:
Suppose I have Objective C interface SomeClass which has a class method called someMethod
Suppose I have used the following code in my objective c iphone programming, [scrollView
I have an NSMutableArray which returns me some object. The object which I added
My objective is to write a program which will call another executable on a
Suppose I have an array having elements am,john,rosa,freedom . I want to compare these
Suppose I’m making an Objective-C class that represents a fraction, and want to create
Objective-C uses a sophisticated message-passing system when one object calls a method on another

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.