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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:26:43+00:00 2026-05-29T10:26:43+00:00

why does my app crash when releasing an array? Ive been trying to figure

  • 0

why does my app crash when releasing an array? Ive been trying to figure it out for HOURS! If I dont release it, it causes a memory leak. If I do release it, it crashes with this error:
Program received signal: “EXC_BAD_ACCESS”.
Sometimes it also says this:
Data Formatters temporarily unavailable, will re-try after a ‘continue’. (Not safe to call dlopen at this time.)

Just cant work it out. Theres alot of code, so i will paste (what i think) is the relevant parts.

-(void)startGame:(int)levelNumber {

    //NOTE there is some code ive not pasted as its too big, but I dont think its relevant

levelsArray = [gameLevels loadLevelMap:levelNumber];
levelsArray_charGuards = [[levelsArray objectAtIndex:4]retain];


    //CREATE CHAR MAIN ########
charMainStore = [[NSMutableArray arrayWithCapacity:1] retain];

charMainToDestroy = [[NSMutableArray arrayWithCapacity:1] retain];

CharMain* charMain = [[CharMain alloc] initWithFrame:CGRectMake(((levels_charStart_x)*40), ((levels_charStart_y)*40), 0, 0)];
[viewController.mapView addSubview:charMain];

[self registerMainChar:charMain];
charMain.gameController = self;
charMain.currentGrid_x = levels_charStart_x;
charMain.currentGrid_y = levels_charStart_y;
charMain.nextGrid_x = levels_charStart_x;
charMain.nextGrid_y = levels_charStart_y;
mapViewController.gameController = self;

[self moveMap_x:((levels_charStart_x)*40) moveMap_y:((levels_charStart_y)*40)];

[charMain release];

 //CREATE CHAR GUARD ######## 
charGuardStore = [[NSMutableArray arrayWithCapacity:10] retain];
charGuardToDestroy = [[NSMutableArray arrayWithCapacity:10] retain]; 
    //These are both @synthesize properly
    //levelsArray_charGuards is from previously

    for (NSMutableArray* levelsArray_charGuards_path in levelsArray_charGuards) {

    NSMutableArray* levelsArray_charGuards_path_eachCoOrd = [levelsArray_charGuards_path objectAtIndex:0];

    int levels_charGuardStart_x = [[NSString stringWithFormat:@"%@",[levelsArray_charGuards_path_eachCoOrd objectAtIndex:0]] intValue];
    int levels_charGuardStart_y = [[NSString stringWithFormat:@"%@",[levelsArray_charGuards_path_eachCoOrd objectAtIndex:1]] intValue];

    CharGuard* charGuard = [[CharGuard alloc] initWithFrame:CGRectMake((levels_charGuardStart_x*40), (levels_charGuardStart_y*40), 0, 0)];
    //CharGuard* charGuard = [[CharGuard alloc] initWithFrame:CGRectMake(((levels_charStart_x)*40), ((levels_charStart_y)*40), 0, 0)];

    [viewController.mapView addSubview:charGuard];

    [self registerGuardChar:charGuard];
    charGuard.gameController = self;
    [charGuard setImage];
    charGuard.currentGrid_x = levels_charGuardStart_x;
    charGuard.currentGrid_y = levels_charGuardStart_y;
    charGuard.nextGrid_x = levels_charGuardStart_x;
    charGuard.nextGrid_y = levels_charGuardStart_y;
    mapViewController.gameController = self;

    charGuard.levelsArray_charGuards_path = levelsArray_charGuards_path;

    [charGuard release];
    charGuard = nil;


    levelsArray_charGuards_path = nil;

    levelsArray_charGuards_path_eachCoOrd =nil;
}
}

When one level is finished, or is restarted, this is called:

-(void)clearLevel {
NSLog(@"Clear Level");

    for (CharMain* charMain in charMainStore){

    [charMain removeFromSuperview];
    [charMainToDestroy addObject:charMain];
}

for (CharMain* charMain in charMainToDestroy){

    [charMainStore removeObject:charMain];

}

for (CharGuard* charGuard in charGuardStore) {

    [charGuard removeFromSuperview];
    [charGuardToDestroy addObject:charGuard];

}

for (CharGuard* charGuard in charGuardToDestroy){

    [charGuardStore removeObject:charGuard];

}

    [charGuardStore release];
charGuardStore=nil;

[charMainStore release];
charMainStore=nil;

//[charGuardToDestroy release]; //If I allow this it crashes!***********
charGuardToDestroy= nil;

[charMainToDestroy release];
charMainToDestroy = nil;

}

The game then calls startGame:(int)levelNumber again.

I have other object which i havnt shown here but they all work fine. Its just the charGuardToDestroy that doesnt want to be released!

I have tried:

-(void)clearLevel {
NSLog(@"Clear Level");

    for (CharMain* charMain in charMainStore){

    [charMain removeFromSuperview];
    [charMainToDestroy addObject:charMain];
}

for (CharMain* charMain in charMainToDestroy){

    [charMainStore removeObject:charMain];

}

for (CharGuard* charGuard in charGuardStore) {

    [charGuard removeFromSuperview];
    //[charGuardToDestroy addObject:charGuard];

}
/*
for (CharGuard* charGuard in charGuardToDestroy){

    [charGuardStore removeObject:charGuard];

}
    */

    [charGuardStore release];
charGuardStore=nil;

[charMainStore release];
charMainStore=nil;

[charGuardToDestroy release]; //I tried allowing this and removing the above code ***********
charGuardToDestroy= nil;

[charMainToDestroy release];
charMainToDestroy = nil;

}

This also worked, but why?? I really need to be able to remove the charGuard from the screen, and from the array, then release it, but I can only do one or the other. Its strange!

  • 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-29T10:26:44+00:00Added an answer on May 29, 2026 at 10:26 am

    Most likely you somewhere added a CharGuard object to the array charGuardStore which is only retained by this array and charGuards superview.

    Here is what I think happens:

    for (CharGuard* charGuard in charGuardStore) {
    
        [charGuard removeFromSuperview];             // charGuard only retained by charGuardStore (= 1 retain left)
        [charGuardToDestroy addObject:charGuard];    // charGuard retained by charGuardToDestroy too (= 2 retains left)
    
    }
    
    for (CharGuard* charGuard in charGuardToDestroy){
    
        [charGuardStore removeObject:charGuard];     // remove 1 retain (= 1 retain left) because it's still in charGuardToDestroy
    
    }
    
    /* more code */
    
    [charGuardToDestroy release];                    // remove all charGuards. charGuard gets released. And most likely you access it later. 
    

    so most likely your bug happens already when you create the CharGuards. Probably overreleasing, but the effect shows up much much later.

    Use NSZombieEnabled to figure out if this is the case.

    I think if you add [charGuardToDestroy removeAllObjects]; after you have removed the charGuards from that array it will crash. This would confirm what I said.

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

Sidebar

Related Questions

I have spend hours trying to figure the following out... I have the following
I'm trying to avoid an app crash here... I have a button that will
I am having a big problem figuring out where my app crash is being
I'm trying to figure out why this code doesn't work. I'm trying to search
I've been trying to debug this issue for hours upon hours but with no
Why does this code crash my app onte emulator and on the device -
I have troubles finding a memory leak which then again causes my App to
This is a continuation of the question here: JBoss - does app have to
The calender app does it... How can I make an app icon change based
It's weird that my app does not zoom in when running in the application

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.