Problem with view resizing when using NSMutableArray

Discussion in 'Mac Programming' started by bahlquist, Oct 27, 2010.

  1. macrumors member

    Joined:
    Oct 6, 2010
    #1
    I am practicing using NSMutableArray. My program makes an array of 25 (NSBezierPath)s and displays them in the view. My program consists of the class BAButtonArray which is as follows:

    BAButtonArray.h:

    Code:
    #import <Cocoa/Cocoa.h>
    
    @interface BAButtonArray : NSView {
    	NSMutableArray* pathArray;
    }
    
    @end
    BAButtonArray.m:

    Code:
    #import "BAButtonArray.h"
    
    @implementation BAButtonArray
    -(void)awakeFromNib{
    	pathArray = [NSMutableArray arrayWithCapacity:25];
    	for(int i = 0 ; i<25 ; i++){
    		NSBezierPath* path = [NSBezierPath bezierPath];
    		[path appendBezierPathWithRect:NSMakeRect((float)(5+ 15*(i%5)), (float)(5 + 15*floor((float)i /5)) , (float)10, (float)10 )];
    		[pathArray addObject:path];
    	}
    }
    
    -(void)drawRect:(NSRect)rect{
    	[[NSColor blackColor] set];
    	[NSBezierPath fillRect:[self bounds]];
    
    	[[NSColor greenColor] set];
    	
    	NSLog(@"starting. . .");
    	for(int i =0 ; i<25 ; i++){
    		[[pathArray objectAtIndex:i] fill];
    	}
    	NSLog(@"done.");
    }
    
    @end
    
    IB information: I dragged a custom view from the palette and made it use BAButtonArray. I fixed the view inside the window so it would resize with constant margin.

    Output: The view draws fine the first time (a grid of 25 green squares in lower left hand corner), but as soon as I try to resize the window, it has problems. The window does not resize at all. Here is the console output:
    Code:
    [Session started at 2010-10-27 14:48:05 -0700.]
    2010-10-27 14:48:06.729 ButtonArray[6305:10b] starting. . .
    2010-10-27 14:48:06.883 ButtonArray[6305:10b] done.
    2010-10-27 14:48:26.474 ButtonArray[6305:10b] starting. . .
    2010-10-27 14:48:26.476 ButtonArray[6305:10b] *** -[NSCFNumber objectAtIndex:]: unrecognized selector sent to instance 0x11cec0
    2010-10-27 14:48:26.535 ButtonArray[6305:10b] *** -[NSCFNumber objectAtIndex:]: unrecognized selector sent to instance 0x11cec0
    2010-10-27 14:48:26.552 ButtonArray[6305:10b] starting. . .
    2010-10-27 14:48:26.565 ButtonArray[6305:10b] *** -[NSCFNumber objectAtIndex:]: unrecognized selector sent to instance 0x11cec0
    2010-10-27 14:48:26.586 ButtonArray[6305:10b] *** -[NSCFNumber objectAtIndex:]: unrecognized selector sent to instance 0x11cec0
    2010-10-27 14:48:28.121 ButtonArray[6305:10b] starting. . .
    2010-10-27 14:48:28.159 ButtonArray[6305:10b] *** -[NSCFNumber objectAtIndex:]: unrecognized selector sent to instance 0x11cec0
    2010-10-27 14:48:28.162 ButtonArray[6305:10b] *** -[NSCFNumber objectAtIndex:]: unrecognized selector sent to instance 0x11cec0
    
    Can you tell me what's wrong with my code?
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
    1. The red-hilited code creates an NSMutableArray that you don't own.
    2. You assign this unowned object to an instance variable: pathArray.
    3. At an unspecified later time, the unowned array will be dealloc'ed because no one has claimed ownership of it. That "unspecified later time" is usually at the next autorelease pool drain, which occurs at the end of the current user event process.
    4. After the NSMutableArray is dealloc'ed, its memory happens to be reused to make an NSCFNumber. That object is not an NSMutableArray, so it doesn't respond to objectAtIndex: messages.

    If ownership, autorelease, etc. don't make sense, then re-read the section of your book that covers memory management and the use of retain and release. Or read this:
    http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
     
  3. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #3
    I knew before coming in here what the problem probably was ;)
     

Share This Page