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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:17:57+00:00 2026-05-26T22:17:57+00:00

If I release my first array after I copied it to the second array

  • 0

If I release my first array after I copied it to the second array my app crashes. If I autorelease the first array everything works fine. Why? Is there a better way to copy the first array to the second array?

If I call this method I get a ECX_BAD_ACCESS, I am passing an empty array

-(NSArray *)loadSystemDetails
{
    AssortedCodeSnippets *acs = [[AssortedCodeSnippets alloc] init];
    NSArray *details;
    NSString *fp = [self tempPathAndFileName:[self systemDetailsFileName]];
    if ([acs fileExistsAtPath:fp]) {
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:fp];
        details = array;
        [array release];
    } else {
        NSLog(@"No File to Load");
        CreateSystem *cls = [[CreateSystem alloc] init];
        details = [cls loadData];
        [cls release];
        [self saveDataFile:details toPath:fp];
    }
    NSLog(@"details: %@",details);
    [acs release];
    return details;
}

If I autorelease array it work fine.

-(NSArray *)loadSystemDetails
{
    AssortedCodeSnippets *acs = [[AssortedCodeSnippets alloc] init];
    NSArray *details;
    NSString *fp = [self tempPathAndFileName:[self systemDetailsFileName]];
    if ([acs fileExistsAtPath:fp]) {
        NSArray *array = [[[NSArray alloc] initWithContentsOfFile:fp]autorelease];
        details = array;
    } else {
        NSLog(@"No File to Load");
        CreateSystem *cls = [[CreateSystem alloc] init];
        details = [cls loadData];
        [cls release];
        [self saveDataFile:details toPath:fp];
    }
  • 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-26T22:17:58+00:00Added an answer on May 26, 2026 at 10:17 pm

    Let’s step through this

    Key: M = release/retain message, C = sum of the release/retain messages

                                                                  // +----+---+
                                                                  // | M  | C |
                                                                  // +----+---+
    NSArray *array = [[NSArray alloc] initWithContentsOfFile:fp]; // | +1 | 1 |
    details = array;                                              // |  0 | 1 |
    [array release];                                              // | -1 | 0 |
                                                                  // +----+---+
    

    At this point you can see that you are going to be returning details which has a count of 0 therefore will have been deallocated already = crash.

    Copy is the wrong term as you do not actually need a copy as such you just want your pointer details to point to a valid object therefore the following would be more correct

    - (NSArray *)systemDetails
    {
        NSString *filePath = [self tempPathAndFileName:[self systemDetailsFileName]];
    
        NSArray *details = [[[NSArray alloc] initWithContentsOfFile:filePath] autorelease];
    
        if (!details) {
    
            NSLog(@"No File to Load");
            CreateSystem *cls = [[CreateSystem alloc] init];
            details = [cls loadData];
            [cls release]; cls = nil;
            [self saveDataFile:details toPath:filePath];
    
        }
    
        NSLog(@"details: %@",details);
    
        return details;
    }
    

    Here I am taking advantage of NSArray‘s method

    initWithContentsOfFile:

    …[returns] nil if the file can’t be opened or the contents of the file can’t be parsed into an array

    This cuts down some of the cruft and makes the method a bit easier to read. I also expanded the variable names to meaningful names (personal preference).

    I have also renamed the method as the load art is superflous as essentially you are returning the system details the fact that they are being loaded is not not really a concern to the caller of the method.

    It’s also important to note that the other answers suggest that you take an additional retain/copy and then remember to release the returned result later on. This goes against cocoa convention as the method name does not contain new/init/copy therefore the callers f the method should not end up owning the result.

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

Sidebar

Related Questions

I am about to release my first app to the iTunes store. What version
After launching our first public alpha release of http://wwww.trademango.com which is built with Play
i release an image with [myimageview.image release]; but after the app was in background
Recently I have finished my first Iphone app and it works grate.... except for
Can someone please explain the following How was the first JDK release unit Tested?
maven release:perform is failing to distribute via FTP. The first thing I checked is
In my function - (void)viewDidLoad : FirstViewController *first = [[FirstViewController alloc]init]; SecondViewController *second =
So I'm making an iOS app that constantly sends and array of points through
i am using NSArray named as allAvailableServices . first i initialize it and after
Every release I find it a good practice to go back and grab all

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.