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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:51:04+00:00 2026-05-18T21:51:04+00:00

Yes i know i should read more about memory management, did try to find

  • 0

Yes i know i should read more about memory management, did try to find and understand, but i still not really get why this bit of code crash when i release the NSMutableArray’s (see code). I do alloc/init initially.

- (void)readSelectedPlayers {
//Prepare File Manager
NSString *filePath = [self dataFilePath];
NSFileManager *fileMgr;
fileMgr = [NSFileManager defaultManager];
//
NSMutableArray *theObjects = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableArray *activePlayersArray = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableArray *readyPlayers = [[NSMutableArray alloc] initWithCapacity:0];
//
// Select all keys from the plist
NSMutableDictionary *playerDict = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
NSArray *allMyKeys = [playerDict allKeys];
//
for(NSString * myKey in allMyKeys) {
    theObjects = [playerDict valueForKey:myKey];

    if ([[theObjects objectAtIndex:1] intValue] == YES) {
        [activePlayersArray addObject:myKey];
    }
}
NSLog(@"activePlayersArray: %@", activePlayersArray);
//
//========CALL AccesQuestionDB MODULE TO SHUFFLE PLAYERS=========//
AccessQuestionsDB *shufflePlayersFunction = [AccessQuestionsDB new];
readyPlayers = [shufflePlayersFunction shufflePlayers: activePlayersArray];
NSLog(@"readyPlayers: %@", readyPlayers);
//
[readyPlayers release];
[theObjects release];
[activePlayersArray release];

}

The ‘//’ between the lines is just to get the code better formatted here.

  • 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-18T21:51:04+00:00Added an answer on May 18, 2026 at 9:51 pm

    You alloc memory for your readyPlayers variable, but then you point it to some other segment of memory:

    readyPlayers = [shufflePlayersFunction shufflePlayers: activePlayersArray];
    

    readPlayer now is autoreleased. You lost the handle to your previously allocated memory.

    What you need to do is principally:

    if (readplayer!= nil){
       [readplayer release];
    }
    readplayer = [[shufflePlayersFunction shufflePlayers: activePlayersArray] retain];
    

    and later… be sure to release that again if not nil when you’re done.

    But… Don’t try doing this!!!
    That’s what you have properties for. Make readyplayer an instance variable, declare a property for it and use synthesize. Now change the alloc to:

    [self setReadyPlayers: [NSMutableArray array]];
    

    set the readplayer by:

    [self setReadyPlayers: [shufflePlayersFunction shufflePlayers: activePlayersArray]];
    

    No need to worry about releasing your array in dealloc etc. let the os do the error prone work for you.
    If readyPlayers really is just a temporary variable, forget about the alloc and the release and let the autorelease handle it.

    [shufflePlayersFunction shufflePlayers: activePlayersArray]
    

    should return an autoreleased array.

    NSMutableArray *readyPlayers = [shufflePlayersFunction shufflePlayers: activePlayersArray];
    NSLog(@"%@", readyPlayers);
    // now forget about it
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, yes I know about this question , but I'm looking for a bit
Yes, I know you could use regular objects as associative arrays in JavaScript, but
Yes, I do understand the difference between them. What I want to know is:
I'm doing some funky authentication work (and yes, I know, open-id is awesome, but
Yes, There's More Than One Way To Do It but there must be a
Yes, I know. The existence of a running copy of SQL Server 6.5 in
Does anyone know if it is possible (and if yes, how) to bind an
I'm working with a large (270+ project) VS.Net solution. Yes, I know this is
...Yes I've seen: Best Resources for Learning JavaFX? but it doesn't really answer the
I've just read on the net about a newly discovered security vulnerability in ASP.NET.

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.