PDA

View Full Version : [NSMutableArray... release];




netytan
Jan 24, 2005, 10:18 AM
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:


[Session started at 2005-01-24 16:06:17 +0000.]

Executable “Strip!” has exited due to signal 10 (SIGBUS).


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;
substring++;
}

for (i = 0; i < substring; i++)
if ([unformattedLine characterAtIndex: i] == '>')
thisIndentationLevel++;

// 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];
[reformattedLines release];

[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.

Mark.



robbieduncan
Jan 24, 2005, 11:10 AM
[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.

netytan
Jan 24, 2005, 11:16 AM
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,

Mark.