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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:45:38+00:00 2026-06-13T19:45:38+00:00

I just noticed a surprising behavior of NSArray , that’s why I’m posting this

  • 0

I just noticed a surprising behavior of NSArray, that’s why I’m posting this question.

I just added a method like:

- (IBAction) crashOrNot
{
   NSArray *array = [[NSArray alloc] init];
   array = [[NSArray alloc] init];
   [array release];
   [array release];
}

Theoretically this code will crash. But In my case it never crashed !!!

I changed the NSArray with NSMutableArray but this time the app crashed.
Why this happens, why NSArray not crashing and NSMutableArray crashes ?

  • 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-13T19:45:40+00:00Added an answer on June 13, 2026 at 7:45 pm

    In general, when you deallocate an object the memory is not zeroed out, it’s just free to be reclaimed by whoever needs it. Therefore if you keep a pointer to the deallocated object, you can usually still use the object for some time (like you do with your second -release message). Sample code:

    #import <Foundation/Foundation.h>
    
    @interface Foo : NSObject
    @property(assign) NSUInteger canary;
    @end
    
    @implementation Foo
    @synthesize canary;
    @end
    
    int main(int argc, const char * argv[])
    {
        @autoreleasepool {
            Foo *foo = [[Foo alloc] init];
            [foo setCanary:42];
            [foo release];
            NSLog(@"%li", [foo canary]); // 42, no problem
        }
        return 0;
    }
    

    There are no checks against this by default, the behaviour is simply undefined. If you set the NSZombieEnabled environment value, the messaging code starts checking for deallocated objects and should throw an exception in your case, just as you probably expected:

    *** -[Foo canary]: message sent to deallocated instance 0x100108250
    

    By the way, the default, unchecked case is one of the reasons why memory errors are so hard to debug, because the behaviour might be highly non-deterministic (it depends on memory usage patterns). You might get strange errors here and there around the code, while the bug is an over-released object somewhere else. Continuing in the previous example:

    Foo *foo = [[Foo alloc] init];
    [foo setCanary:42];
    [foo release];
    Foo *bar = [[Foo alloc] init];
    [bar setCanary:11];
    NSLog(@"%li", [foo canary]); // 11, magic! (Not guaranteed.)
    

    As for why is NSArray different from NSMutableArray, an empty array looks like a special beast indeed:

    NSArray *foo = [[NSArray alloc] init];
    NSArray *bar = [[NSArray alloc] init];
    NSLog(@"%i", foo == bar); // yes, they point to the same object
    

    So that might have something to do with it. But in general case, working with deallocated objects might do anything. It might work, it might not, it might spill your coffee or start a nuclear war. Don’t do it.

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

Sidebar

Related Questions

I just noticed that by instance famous application like chrome didn't have any 64
I just noticed a strange behavior which looks like a bug. Consider the following
I just noticed this bevahior in Hibernate and found it a bit surprising (in
I just noticed that I could use an a variable as an argument, like
I just noticed something that I've never realised before. It turns out that this
This question is just out of general curiosity. I've just noticed it when working
I just noticed that you can do this in C#: Unit myUnit = 5;
I just noticed while creating a RESTful WCF service that the Method parameter on
I just noticed a behavior in argparse that puzzled me (guess I'd never used
I just noticed that when you try to generate a method stub on code

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.