ObC:应用程序在返回NSMutableArray后崩溃了?

时间:2010-09-12 14:04:24

标签: objective-c

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

  1. 启动应用
  2. 按按钮=加载新视图
  3. 在新的viewDidLoad中我调用另一个对象/函数并发送一个NSMutableArray
  4. 处理数据并发回NSMutableArray
  5. 应用程序崩溃,请参阅评论位置。大多数情况下,当我再次返回时,但有时是第一次
  6. 由于我是新手,我想我做了很多这个错误,但有人可以看看代码并给我一些建议。我会假设我在发布内容时遇到问题。

    - (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
    

2 个答案:

答案 0 :(得分:1)

你过度释放了readDataArray(在没有创建它的方法中称为propArray1)。您创建它并在第二种方法中自动释放它,然后在第一种方法结束时再次释放它(未创建的地方)。

答案 1 :(得分:0)

我建议您使用最新XCode附带的Analyze功能。这是一个很好的功能,我总是用来跟踪我是否忘记释放或释放太多。

我还发现你也过度释放propertiesArray,因为它包含来自[fileString componentsSeparatedByString:]的结果,根据Cocoa惯例,它将自动释放。