Objective c ObC:返回NSMutableArray后应用程序崩溃?

Objective c ObC:返回NSMutableArray后应用程序崩溃?,objective-c,Objective C,我是ObC的新手,有一个我无法解决的问题。可能还有其他问题,但主要问题是: 启动应用程序 按下按钮=加载新视图 在新的viewDidLoad中,我调用另一个对象/函数并发送一个NSMutableArray 处理数据并发回NSMutableArray 应用程序崩溃,请参见注释。大多数时候,当我一次又一次地回去,但有时是第一次 因为我是新来的,我想我做了很多错事,但是有人能看看代码并给我一些建议吗。我想我在发布一些东西方面有问题 - (void)viewDidLoad { [super vie

我是ObC的新手,有一个我无法解决的问题。可能还有其他问题,但主要问题是:

  • 启动应用程序
  • 按下按钮=加载新视图
  • 在新的viewDidLoad中,我调用另一个对象/函数并发送一个NSMutableArray
  • 处理数据并发回NSMutableArray
  • 应用程序崩溃,请参见注释。大多数时候,当我一次又一次地回去,但有时是第一次
  • 因为我是新来的,我想我做了很多错事,但是有人能看看代码并给我一些建议吗。我想我在发布一些东西方面有问题

    - (void)viewDidLoad {
       [super viewDidLoad];
    
        NSLog(@" ");
        NSLog(@"viewDidLoad ");
        NSLog(@" ");
        NSLog(@">>Processing prepareGame<<");
    
        NSMutableArray *propArray1 = [[NSMutableArray alloc] initWithObjects:@"9999", nil]; //Init with dummy numbers
    
        AccessPropertiesFile *readMyProperties = [AccessPropertiesFile new]; //Init function call to read file
    
        NSLog(@"Prepare to call readProperties");
    
        propArray1 = [readMyProperties readPropertiesFile:propArray1];
        NSLog(@"Back from readProperties:error after this");
        /*
        for (NSString *element in propArray1) {
            NSLog(@"Elements in prop2Array; %@", element);
        }
        */ 
        [readMyProperties release];
        [propArray1 release];
    }
    
    
    
    -(NSMutableArray *)readPropertiesFile:(NSMutableArray *)readDataArray {
    
        NSLog(@"Processing readProperties");
    
        // For error information
        NSError *error;
        //Prepare File Manager
        NSString *filePath = [self dataFilePath];
        NSFileManager *fileMgr;
        fileMgr = [NSFileManager defaultManager];
        NSArray *propertiesArray = [NSArray alloc]; //Alloc array
    
        //Check from what module the call is coming from to ecide what to do
        if ([fileMgr fileExistsAtPath: filePath] == NO) {
            NSLog (@"File not found");
            //File does not exists, this is the first time the game starts
            //Set up default parameters
            NSString *fileString =@"0\n30\n30\n10\n1\n1\n1\n2\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n";
            // Write default parameters to file
            [fileString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
            propertiesArray = [fileString componentsSeparatedByString:@"\n"]; // each line, adjust character for line endings
    
        }
        else {      //File exists
            NSLog (@"File exists");
            NSString *fileString = [NSString stringWithContentsOfFile:filePath
                                                             encoding:NSUTF8StringEncoding error:nil]; // reads file into memory as an NSString
            propertiesArray = [fileString componentsSeparatedByString:@"\n"]; // each line, adjust character for line endings
    
        }
    
        //Clean readDataArray
        [readDataArray removeAllObjects];
    
        //Populate return array
        for (NSString *element in propertiesArray) {
            //NSLog(@"Elements in propertiesArray; %@", element);
    
            [readDataArray addObject:element];
    
        }
        NSLog(@"readDataArray: %@", readDataArray);
    
    
        [propertiesArray release];
        [readDataArray autorelease];
    
        NSLog(@"returning from readProperties");
    
        return readDataArray;
    }
    
    @end
    
    -(void)viewDidLoad{
    [超级视图下载];
    NSLog(@“);
    NSLog(@“viewDidLoad”);
    NSLog(@“);
    
    NSLog(@“>>Processing PrepareName您过度释放了readDataArray(在未创建它的方法中称为propArray1)。您创建了它,并在第二个方法中自动释放它,然后在第一个方法(未创建的方法)的末尾再次释放它.

    我建议您使用最新XCode附带的分析功能。这是一个很好的功能,如果我忘记发布或发布太多,我总是使用它来跟踪


    我还发现您还过度发布了
    属性数组
    ,因为它包含
    [fileString components由string:]分离的结果
    ,它将根据Cocoa约定自动释放。

    谢谢,我在第一个方法中使用viewDidLoad将其删除,但在第二次运行该方法时它仍然会中断。当我进入“新游戏”视图(viewDidLoad)时,第一个方法会激活然后,我按照上面的方法进行处理,完成后,我按“上一步”按钮返回。第二次转到“newGame”时,它会中断。这段代码中有许多奇怪之处,表明对内存管理的掌握不太好。例如,您对readDataArray的处理(以及通常的readPropertiesFile方法)非常奇怪。我建议查看内存管理文档并重新思考您的总体方法。非常感谢,似乎是过度发布的属性阵列导致了问题。目前看来我的问题已经解决:-)