NSMutableArray error

Discussion in 'Mac Programming' started by larswik, Aug 2, 2011.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    In my AppController I have a method called 'awakeFromNib' which loads everything as the program launches. Last night I added to the awakeFromNib a NSMutableArray using this code bellow, it's a list for my RolePlaying to store kills.
    Code:
     'deathList = [[NSMutableArray alloc] init];'
    In the header I placed NSMutableArray *deathList;
    I have an NSButton that will take everything in the NSTextFields, put them in to NSStrings and box it in an object and store it in my deathList mutable array. It only works when I add and instantiate the deathList from within this code in the NSButton which I am hiding with the // right now.
    Code:
    - (IBAction)DeathCertCloseAndSaveButton:(id)sender {
        MakeDeathCertificate *newEntry = [[MakeDeathCertificate alloc]init]; //I instantiate a the class
        //deathList = [[NSMutableArray alloc] init];
        
        theNameField = [nameOfTheDead stringValue]; // Get the value that in in my NSTextField
        [deathList addObject:[newEntry returnTheDeathCertificate: theNameField]]; //Send the NSString to process and return an object to MutableArray
        
        DeathCertItem *testPrint = [deathList objectAtIndex:0];
        NSLog(@"Test output %@", [testPrint name]);
    }
    If I put that line of code in my awakeFromNib Method I get this error when it returns the object from the NSButton method I set up
    I have not released deathList. Since deathList is in the main header file it should not be a local problem and should see it? But I am guessing it is acting as it's been released and it can't find it, I am guessing?
     
  2. holmesf macrumors 6502a

    Joined:
    Sep 30, 2001
    #2
    Hard to say what the problem is. Perhaps post the complete code.
     
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    The whole thing is like 900 lines of code. I wrote it before I had a good understanding of objects so it is more or less procedural.

    It's almost like the NSMutableArray is not there even thought I declared it in the @interface section and instantiated it in the within awakeFromNib Method.

    I am going to try to use an IF statement to instantiate it within the method for that NSButton Action. If it sees it use it if not instantiate it.

    It's ood.

    Thanks
     
  4. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #4
    I found the mistake. Totally my fault. When I last had the program open weeks ago I started on a list and called it the same thing 'deathList' It tried to instantiate 2 times in 2 different locations. I did a search through the code for the word 'deathList' and that is where I found the other entry, where I also dealloc it. That explains why it worked when I re instantiated it within the method as a local variable and not from the awakeFromNib method.

    It took 8 hours to find that mistake, but I found it.
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    You should never dealloc any object other than self. Never.

    If it's not clear, then you should reread the Memory Management Guide, under the heading "Implement dealloc to Relinquish Ownership of Objects":
    Important: You should never invoke another object’s dealloc method directly.

    If you meant release rather than dealloc, then all I can say is "Accuracy is important."

    If you were actually doing a release, then you might have found the bug by running your app under Instruments, with zombies enabled.
     
  6. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #6
    ahhh, I even wrote it wrong :) I did not me dealloc I meant I released it, [deathList release]; When the method in question exited I had a release for that object. When I sent a message to it from another method I got the error, because it was released.

    I do have a memory issue, but it is not having to re read anything, it is my memory :) So much to remember and being redundant with these small projects helps it sync it.
     

Share This Page