[NSMutableArray... release];

Discussion in 'Mac Programming' started by netytan, Jan 24, 2005.

  1. netytan macrumors 6502


    May 23, 2004
    I'm a little lost,

    I have a NSMutableArray instance which gets released at the end of my IBAction. The problem is that if I do release it the program terminates with:

    Now if I remove the release message everything is fine, but that raises the question: shouldn't I release the instances that I create? Heres the action as I have it; not finished ;).

    #import "StripController.h"
    @implementation StripController
    - (IBAction)clean:(id)sender
        int thisIndentationLevel = 0,
            lastIndentationLevel = 1,
            lineIndex, lineCount;
        NSMutableArray *reformattedLines;
        NSArray *unformattedLines;
        NSMutableString *reformattedLine;
        NSString *unformattedLine;
        unformattedLines = [[[textView string]
                                componentsSeparatedByString: @"\n"] retain];
        lineCount = [unformattedLines count];
        reformattedLines = [NSMutableArray array];
        for (lineIndex = 0; lineIndex < lineCount; lineIndex++)
            thisIndentationLevel = 0;
            // Begin the main formatting loop. Loops over each line in *textView and cleans
            // the source to make it easier to read.
            int i, c, substring = 0; 
            unformattedLine = [unformattedLines objectAtIndex: lineIndex];
            for (i = 0, c = [unformattedLine length]; i < c; i++)
                if ([unformattedLine characterAtIndex: i] != '>' &
                    [unformattedLine characterAtIndex: i] != ' ') break;
            for (i = 0; i < substring; i++)
                if ([unformattedLine characterAtIndex: i] == '>')
            // Make changes to the string and append the newly formatted line to the
            // reformattedLines variable.
            lastIndentationLevel = thisIndentationLevel;
        // Update text in the *textView outlet.
        [unformattedLines release];
        [b][reformattedLines release];[/b]
        [sender setEnabled: NO];
    The bold section causes the problem, if anyone could explain why this is happening I would much appreciate it! I've check the retainCount right before the release (it's 1), and no calls are being made to the instance after its release as far as I can see :confused:.

    Edit: I could the code project if that would help, but this is pretty much it appart from a simple interface and a few methods like windowWillClose: etc.

    Thanks a lot, you guys are always very helpful.

  2. robbieduncan Moderator emeritus


    Jul 24, 2002
    [NSMutableArray array]
    returns an autoreleases mutable array. You don't need to (and should not) release it. In general all methods like this return autoreleased objects whereas alloc init returns objects with retain count 1.
  3. netytan thread starter macrumors 6502


    May 23, 2004
    I was just reading the through the "Memory Managment" article... you know that feeling that you get when you suddenly realise you've been a total moron ;). Am I right in assuming that componentsSeparatedByString: also returns an autorelease object. If this is the case I won't bother retaining/releasing this one too :rolleyes:

    Thanks again Robbie,


Share This Page