开发者

NSMutableArray as an Instance Variable

开发者 https://www.devze.com 2023-03-29 17:41 出处:网络
I have a instance variable which is a NSMutableArray @interface SummaryWindowController : NSWindowController {

I have a instance variable which is a NSMutableArray

@interface SummaryWindowController : NSWindowController {

    NSMutableArray *aBuffer;

The NSMutableArray is set using this method (called from the object that init'ed this object):

- (void)setGlobalStatusArray:(NSMutableArray *)myArray
{
    if (!aBuffer) {
        [myArray retain];
        NSLog(@"aBuffer not init , alloc init now");
        aBuffer = [[NSMutableArray alloc] initWithArray:myArray];
    }
    NSLog(@"--> Received buffer: %@",aBuffer);

}

The NSLog shows the contents of the array when that method runs:

2011-08-18 16:00:26.052 AppName[74751:1307] --> Recievied buffer: (
        {
        discription = DiskUsage;
        menu = "<NSMenuItem: 0x1005116e0 Hardware Status>";
        status = Warning;
    },

But in my method that uses this instance variable it no longer seems init'ed

- (IBAction)refreshButtonClicked:(id)sender
{
    NSLog(@"The user has clicked the update button");
    if (!aBuffer) {
        NSLog(@"refresh button not init");
    }
    NSLog(@"Buffer is currently:%@",aBuffer);
}

As when it gets to this point I see the following NSLog:

2011-08-18 16:04:25.301 AppName[74829:1307] The user has clicked the update button
2011-08-18 16:04:25.303 AppName[74829:1307] refresh button not init
2011-08-18 16:04:25.304 AppName[74829:1307] Buffer is currently:(null)

Which would indicate to me that aBuffer has been (auto?)released?

Any ideas why this would be doing this? I thought at first I had two distinct objects, one which I created by initing NSWindowController from the original controller:

   @interface AppName_AppDelegate : NSObject 
    NSMutableArray *globalStatusArray;

    @implementation AppName_AppDelegate

    if ( summaryWindow ) {
            [summaryWindow release];
        } // end if
        summaryWindow   = [[SummaryWindowController alloc] initWithWindowNibName:@"SummaryWindow" owner:globalStatusController];
    [summaryWind开发者_JAVA技巧ow showWindow:self];
    [summaryWindow setGlobalStatusArray:globalStatusArray];

And one that was created when the nib load, identical but different objects however I now don't think thats the case as I don't see duplicate NSLogs any more, so I assume its just some basic memory issue with NSMutableArray(s)?


You are retaining myArray when you should be retaining aBuffer.


Try using an

@property (nonatomic, retain) NSMutableArray *aBuffer

in your interface.

Next,

 @synthesize aBuffer

In your .m file. AFter that,

when you set your array, do it like this:

    self.aBuffer = [[NSMutableArray alloc] initWithArray:myArray];

Note the "self" so that you are specifying the property.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号