Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 24, 2005, 10:18 AM   #1
netytan
macrumors 6502
 
Join Date: May 2004
[NSMutableArray... release];

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:

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

Code:
#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 .

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.
__________________
12" G4 IBook (1GHz) named Darwin: 60GB HardDrive, BlueTooth, Airport Extreme coming soon?

Last edited by netytan; Jan 24, 2005 at 10:26 AM.
netytan is offline   0 Reply With Quote
Old Jan 24, 2005, 11:10 AM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Code:
[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.
robbieduncan is offline   0 Reply With Quote
Old Jan 24, 2005, 11:16 AM   #3
netytan
Thread Starter
macrumors 6502
 
Join Date: May 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

Thanks again Robbie,

Mark.
__________________
12" G4 IBook (1GHz) named Darwin: 60GB HardDrive, BlueTooth, Airport Extreme coming soon?

Last edited by netytan; Jan 24, 2005 at 11:18 AM.
netytan is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
nsmutablearray tacotester1 iPhone/iPad Programming 6 Apr 10, 2014 04:18 PM
NSMutableArray always replacing last value. DavidBlack Mac Programming 16 Dec 15, 2013 03:49 PM
Objective-C help with NSMutableArray dancks Mac Programming 4 Apr 15, 2013 04:25 PM
NSArray to NSMutableArray larswik Mac Programming 3 Feb 19, 2013 10:10 PM
What is the difference between NSArray and NSMutableArray anandkumar45 iPhone Tips, Help and Troubleshooting 1 Nov 2, 2012 05:09 AM

Forum Jump

All times are GMT -5. The time now is 04:51 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC