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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:29:21+00:00 2026-05-16T17:29:21+00:00

For some reason when I release the NSArray I get the EXC_BAD_ACCESS exception. Here

  • 0

For some reason when I release the NSArray I get the EXC_BAD_ACCESS exception. Here is the implementation:

-(void) loadAllAlphabets
{
    NSBundle *bundle = [NSBundle mainBundle]; 
    NSArray *imagesPath = [[NSArray alloc] init]; 

    imagesPath = [bundle pathsForResourcesOfType:@"png" inDirectory:@"Images"];

    alphabets = [[NSMutableArray alloc] init]; 

    NSString *fileName = [[NSString alloc] init]; 

    for(int i=0; i<= imagesPath.count -1 ; i++) 
    {
        fileName = [[imagesPath objectAtIndex:i] lastPathComponent];
        CCSprite *sprite = [CCSprite spriteWithFile:fileName];

        sprite.userData = [[fileName stringByDeletingPathExtension] uppercaseString];   

        [alphabets addObject:sprite]; 
    }

    // release fileName 
    [fileName release]; 
    fileName = nil; 

    [imagesPath release]; // this causes the application to crash with EXC_BAD_ACCESS
//  imagesPath = nil; 

}

UPDATE 1:

So, the problem was that although I was responsible for releasing the imagesPath object since I used alloc that soon become irrelevant when pathsForResourcesOfType returned an autorelease object.
This means I should not release the imagesPath object manually.

The following line should be used:

NSArray *imagesPath = [bundle pathsForResourcesOfType:@"png" inDirectory:@"Images"];

UPDATE 2:

Another question which is related to this post. In the following code I initialize a new NSMutableArray manually.

alphabets = [[NSMutableArray alloc] init]; 

Later I insert CCSprite (Cocos2d objects) into alphabets array. CCSprite are autorelease objects. Do I still have to release alphabets manually? Since, after some time all objects are released and memory will be returned but then what will be left inside alphabets NSMutable array?

  • 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-16T17:29:22+00:00Added an answer on May 16, 2026 at 5:29 pm

    general rule of thumb in memory management – you should release an object only if you obtain it using method that contains new, copy or alloc in it (standard method follow that rule and you should stick to it as well).

    In your case you obtain imagesPath object using pathsForResourcesOfType: method which returns an autoreleased object so you must not release it yourself.

    Edit: yes, you need to release alphabets object somewhere (for the same reason – you got it with alloc method).

    Objective-c containers take an ownership of objects added to them, that us when objects are added to an array they get retained so it is guaranteed that their life time is at least as long as the life time of container. When you remove object from collection or collection itself is destroyed then its members get released (to compensate retain on add).

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

Sidebar

Related Questions

For some reason, the retain/release behavior in the following code has me completely baffled.
For some reason Visual Studio 2008 doesn't always stop when an exception occurs to
unsure how to go about describing this but here i go: For some reason,
I'm trying to add objects to this NSArray (labelArray) but for some reason, it
I just installed Eclipse Indigo (Service Release 1). For some reason my Available Software
I'm trying to sort an album by release date. For some reason I am
For some reason, this line of code is returning undefined for $(this).attr(href) $(a).attr(href, javascript:page('
for some reason when I try to call CocoaAsyncSocket's onSocket:didReadData:withTag method, it's failing and
For some reason I can't detect I can't set a z index for any
For some reason there's a variable called d that is defined immediately after 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.