内存泄漏问题,我需要帮助#2

时间:2011-04-18 16:13:35

标签: iphone objective-c memory-leaks

  

可能重复:
  Memory leak problem and i need help #1

您好,

我发现这个泄漏确实有问题,目前正在花费很多时间。

我有一个名为“AccessQuestionsDB.m”的类。在本课程中,我有以下代码:

- (NSMutableArray *)readQuestion: (int)questionNr {
NSLog(@">>readQuestion<<");

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:&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];
}

在上面的代码中,我有以下几行:

//=========PREPARE CORE DATA DB===========//
if (managedObjectContext == nil) { managedObjectContext = [(FamQuiz_R0_1AppDelegate *)
                                                           [[UIApplication sharedApplication] delegate] managedObjectContext]; }

managedObjectContext在.h文件中定义:

NSManagedObjectContext *managedObjectContext;

我在这个.m文件中的两个附加模块上有相同的if语句,这没有问题。但是,我永远不会发布。

当我发布时,我当然遇到了麻烦。我确实尝试更改三个名称并发布,所以有唯一的名称,但这不起作用。

这可能是我在这个.m文件中泄漏的原因吗?

当我被困住,成为新人时,我需要任何帮助,因为我真的不知道如何找到除此之外的实际泄漏。

==== UPDATE =====

这让我发疯,尤其是因为它指向不同的地方。但是,我已经取出了readQuestion并将其放在自己的类中。所以现在整个.m看起来像:

//

#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(@">>createQuestionsList<<");

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:&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 < nrOfQuestions; oo++) {
        [allJointQ addObject:[allDummyQ objectAtIndex:oo]];
    }
    [masterQuestionsArray addObjectsFromArray:allJointQ];
}
else {                  // Use diff

    //===SELECT EASY QUESTIONS===//
    for (int oo = 0; oo < (nrOfEasyPlayers * nrOfQuestions); oo++) {
        [easyQ addObject:[allEasyArrayQ objectAtIndex:oo]];
    } 

    //===SELECT MEDIUM QUESTIONS===//
    for (int oo = 0; oo < (nrOfMediumPlayers * nrOfQuestions); oo++) {
        [mediumQ addObject:[allMediumArrayQ objectAtIndex:oo]];
    }

    //===SELECT HARD QUESTIONS===//
    for (int oo = 0; oo < (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 > 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

Instruments指出泄漏的那条线现在是:

  

NSArray * fetchedObjects = [qContext executeFetchRequest:fetchRequest error:&amp; error];

这是第79行,它表示100%。

0 个答案:

没有答案