title: Memory leak problem and i need help #2
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/5696471/memory-leak-problem-and-i-need-help-1">Memory leak problem and i need help #1</a>  </p>
  </blockquote>
  
  
  
  <p>Hi,</p>
  
  <p>I really have problem to find this leak and is currently putting a lot of hours into this.</p>
  
  <p>I have a class that is called "AccessQuestionsDB.m". In this class i have the following code:</p>
  
  <pre><code>- (NSMutableArray *)readQuestion: (int)questionNr {
  NSLog(@"&gt;&gt;readQuestion&lt;&lt;");
  
  NSMutableArray *read_Question = [[NSMutableArray alloc] initWithCapacity: 0];
  
  NSError *error;
  //=========PREPARE CORE DATA DB===========//
  if (managedObjectContext == nil) { managedObjectContext = [(FamQuiz_R0_1AppDelegate *)
                                                             [[UIApplication sharedApplication] delegate] managedObjectContext]; }
  // Define qContext
  NSManagedObjectContext *qContext = [self managedObjectContext];
  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  NSEntityDescription *entity = [NSEntityDescription 
                                 entityForName:@"questions" inManagedObjectContext:qContext];
  [fetchRequest setEntity:entity];
  
  NSArray *fetchedObjects = [qContext executeFetchRequest:fetchRequest error:&amp;error];
  for (NSManagedObject *info in fetchedObjects) {
      if ([[info valueForKey:@"idQ"] intValue] == questionNr) { 
          [read_Question addObject:[info valueForKey:@"question"]];
          [read_Question addObject:[info valueForKey:@"qRightAnswer"]];
          [read_Question addObject:[info valueForKey:@"qWrongAnswer1"]];
          [read_Question addObject:[info valueForKey:@"qWrongAnswer2"]];
      }
  } 
  
  [read_Question addObject:@"XXXX"]; // Here to test a suggestion from someone to find the leak
  [read_Question removeLastObject]; // Here to test a suggestion from someone to find the leak
  
  [fetchRequest release];
      return [read_Question autorelease];
  }
  </code></pre>
  
  <p>In the above code i have the following lines:</p>
  
  <pre><code>//=========PREPARE CORE DATA DB===========//
  if (managedObjectContext == nil) { managedObjectContext = [(FamQuiz_R0_1AppDelegate *)
                                                             [[UIApplication sharedApplication] delegate] managedObjectContext]; }
  </code></pre>
  
  <p>managedObjectContext is defined in the .h file:</p>
  
  <pre><code>NSManagedObjectContext *managedObjectContext;
  </code></pre>
  
  <p>I do have the same if-statement on two additional modules in this .m file, which works without problem. However, i never do release.</p>
  
  <p>When i did release i got into trouble, of course. I did try to change the names on the three and do release so there was unique names but that did not work. </p>
  
  <p>Could this be the reason for the leak i have in this .m file?</p>
  
  <p>As i am stuck, being new and all, i need any help i can get as i do not really know how to find the actual leak other than this.</p>
  
  <p>====UPDATE=====</p>
  
  <p>This is driving me crazy, especially as it points towards different places. However, i have taken out readQuestion piece and put that in its own class. So now the whole .m looks like:</p>
  
  <pre><code>//
  
  #import "AccessQuestionsDB.h"
  #import "FamQuiz_R0_1AppDelegate.h"
  #import "AccessCurrentGameData.h"
  
  @implementation AccessQuestionsDB
  
  //@synthesize fetchedResultsController, managedObjectContext;
  @synthesize managedObjectContext;
  
  @synthesize allEasyArrayQ;
  @synthesize allMediumArrayQ;
  @synthesize allHardArrayQ;
  @synthesize allDummyQ;
  @synthesize allJointQ;
  @synthesize easyQ;
  @synthesize mediumQ;
  @synthesize hardQ;
  @synthesize masterQuestionsArray;
  //@synthesize read_Question;
  
  - (NSMutableArray *)getPlayerLevelsAndQuestions {
  NSString *filePath2 = [self dataFilePath2];
  NSMutableArray *playersAndQuestionsArray = [[NSArray alloc] initWithContentsOfFile:filePath2];
  
  [playersAndQuestionsArray autorelease];
  
  return playersAndQuestionsArray;
  }
  
  
  - (void)createQuestionsList: (NSMutableArray *)diffArray {
  NSLog(@"&gt;&gt;createQuestionsList&lt;&lt;");
  
  int nrOfQuestions = [[diffArray objectAtIndex:0]intValue];
  int nrOfEasyPlayers = [[diffArray objectAtIndex:1]intValue];
  int nrOfMediumPlayers = [[diffArray objectAtIndex:2]intValue];
  int nrOfHardPlayers = [[diffArray objectAtIndex:3]intValue];
  int nrOfPlayers = nrOfEasyPlayers + nrOfMediumPlayers + nrOfHardPlayers;
  
  allHardArrayQ = [[NSMutableArray alloc]init];
  allMediumArrayQ = [[NSMutableArray alloc]init];
  allEasyArrayQ = [[NSMutableArray alloc]init];
  allDummyQ = [[NSMutableArray alloc]init];
  allJointQ = [[NSMutableArray alloc]init];
  hardQ = [[NSMutableArray alloc]init];
  mediumQ = [[NSMutableArray alloc]init];
  easyQ = [[NSMutableArray alloc]init];
  
  masterQuestionsArray = [[NSMutableArray alloc] initWithObjects:
                          [NSNumber numberWithBool:[[diffArray objectAtIndex:4]boolValue]],
                          [NSNumber numberWithInt:nrOfHardPlayers],
                          [NSNumber numberWithInt:nrOfMediumPlayers],
                          [NSNumber numberWithInt:nrOfEasyPlayers],
                          [NSNumber numberWithInt:nrOfQuestions],
                          nil];
  
  NSError *error;
  
  //=========PREPARE CORE DATA DB===========//
  if (self.managedObjectContext == nil) { self.managedObjectContext = [(FamQuiz_R0_1AppDelegate *)
                                                             [[UIApplication sharedApplication] delegate] managedObjectContext]; }
  
  
  // Define qContext
  NSManagedObjectContext *qContext = [self managedObjectContext];
  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  NSEntityDescription *entity = [NSEntityDescription 
                                 entityForName:@"questions" inManagedObjectContext:qContext];
  [fetchRequest setEntity:entity];
  
  NSArray *fetchedObjects = [qContext executeFetchRequest:fetchRequest error:&amp;error];
  for (NSManagedObject *info in fetchedObjects) {
      if ([[info valueForKey:@"qDiff"] intValue] == 1) { 
          [allEasyArrayQ addObject:[info valueForKey:@"idQ"]];
      } else if ([[info valueForKey:@"qDiff"] intValue] == 2) { 
          [allMediumArrayQ addObject:[info valueForKey:@"idQ"]];
      } else if ([[info valueForKey:@"qDiff"] intValue] == 3) { 
          [allHardArrayQ addObject:[info valueForKey:@"idQ"]];
      }
  }
  
  //===SHUFFLE QUESTIONS THREE TIMES===//
  [allEasyArrayQ shuffle];
  [allMediumArrayQ shuffle];
  [allHardArrayQ shuffle];
  [allEasyArrayQ shuffle];
  [allMediumArrayQ shuffle];
  [allHardArrayQ shuffle];
  [allEasyArrayQ shuffle];
  [allMediumArrayQ shuffle];
  [allHardArrayQ shuffle];
  
  if ([[diffArray objectAtIndex:4]boolValue] == NO) {
  
      //=====CHECK WHAT DIFF-LVLs SHOULD PARTICIPATE====//
      if ([[diffArray objectAtIndex:5]boolValue] == YES) {
          [allDummyQ addObjectsFromArray:allEasyArrayQ];
      }
      if ([[diffArray objectAtIndex:6]boolValue] == YES) {
          [allDummyQ addObjectsFromArray:allMediumArrayQ];
      }
      if ([[diffArray objectAtIndex:7]boolValue] == YES) {
          [allDummyQ addObjectsFromArray:allHardArrayQ];
      }
  
      //====SHUFFLE THREE TIMES====//
      [allDummyQ shuffle];
      [allDummyQ shuffle];
      [allDummyQ shuffle];
  
      nrOfQuestions = nrOfQuestions * nrOfPlayers;
  
      //====SELECT THE RIGHT AMOUNT OF QUESTIONS====//
      for (int oo = 0; oo &lt; nrOfQuestions; oo++) {
          [allJointQ addObject:[allDummyQ objectAtIndex:oo]];
      }
      [masterQuestionsArray addObjectsFromArray:allJointQ];
  }
  else {                  // Use diff
  
      //===SELECT EASY QUESTIONS===//
      for (int oo = 0; oo &lt; (nrOfEasyPlayers * nrOfQuestions); oo++) {
          [easyQ addObject:[allEasyArrayQ objectAtIndex:oo]];
      } 
  
      //===SELECT MEDIUM QUESTIONS===//
      for (int oo = 0; oo &lt; (nrOfMediumPlayers * nrOfQuestions); oo++) {
          [mediumQ addObject:[allMediumArrayQ objectAtIndex:oo]];
      }
  
      //===SELECT HARD QUESTIONS===//
      for (int oo = 0; oo &lt; (nrOfHardPlayers * nrOfQuestions); oo++) {
          [hardQ addObject:[allHardArrayQ objectAtIndex:oo]];
      }
  
      [masterQuestionsArray addObjectsFromArray:hardQ];
      [masterQuestionsArray addObjectsFromArray:mediumQ];
      [masterQuestionsArray addObjectsFromArray:easyQ];
  
  }
  
  //======SAVE TO FILE=======//
  
  //Prepare File Manager
  NSString *filePath2 = [self dataFilePath2];
  [masterQuestionsArray writeToFile:filePath2 atomically: TRUE];
  
  [allEasyArrayQ release];
  [allMediumArrayQ release];
  [allHardArrayQ release];
  [allDummyQ release];
  [allJointQ release];
  [easyQ release];
  [mediumQ release];
  [hardQ release];
  [masterQuestionsArray release];
  [fetchRequest release];
  
  //  [managedObjectContext release];
  
   }
  
  
  - (NSMutableArray *)askForQuestionNumbers {
  
  //====GET DATA TO BE ABLE TO SELECT QUESTIONS====//
  AccessCurrentGameData *askForPlayerInfoFunction = [AccessCurrentGameData new];
  //  NSMutableArray *playersInGameAndTheirDiff = [askForPlayerInfoFunction askForQuestionNumbers];
  [askForPlayerInfoFunction release];
  
  return 0;
  }
  
  - (NSMutableArray *)shufflePlayers: (NSMutableArray *)playerList {
  [playerList shuffle];
  return playerList;
  }
  
  //====HERE THE POPULATE DB SHOULD RESIDE=====//
  
  
  -(NSString *)dataFilePath {
  NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [path objectAtIndex:0];
  return [documentsDirectory stringByAppendingPathComponent:kGameSETUP_PLIST];
  //return @"/Users/PeterK/Desktop/gamesetup.plist";
  }
  
  -(NSString *)dataFilePath2 {
  NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [path objectAtIndex:0];
  return [documentsDirectory stringByAppendingPathComponent:kMasterQUESTIONS_PLIST];
  }
  
  @end
  
  // =========SHUFFLE==========//
  @implementation NSMutableArray (Shuffle)
  - (void) shuffle {
  for (NSInteger i = [self count] - 1; i &gt; 0; --i) {
      [self exchangeObjectAtIndex: arc4random() % (i+1) withObjectAtIndex: i];
  }
  }
  @end
  
  
  
  @implementation NSArray (Shuffle)
  - (NSArray*) shuffledArray {
  NSMutableArray* shuffledArray = [NSMutableArray arrayWithArray: self];
  [shuffledArray shuffle];
  return shuffledArray;
  }
  @end
  </code></pre>
  
  <p>The line that Instruments points out as the one leaking now is:</p>
  
  <blockquote>
    <p>NSArray *fetchedObjects = [qContext executeFetchRequest:fetchRequest error:&amp;error];</p>
  </blockquote>
  
  <p>This is line 79 and it indicate 100%.</p>