1

我有一個實例變量這是一個的NSMutableArrayNSMutableArray的作爲變量的實例

@interface SummaryWindowController : NSWindowController { 

    NSMutableArray *aBuffer; 

的NSMutableArray中使用該方法(從init'ed此對象的對象調用)設置:

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

} 

所述的NSLog示出了數組的內容時方法運行:

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

但在米使用這個實例變量似乎不再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); 
} 

當它到達這一點,我看到下面的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) 

這將表明,我認爲aBuffer一直(自動?)釋放?

任何想法,爲什麼會這樣做?我開始還以爲我有兩個不同的對象,一個是我從原來的控制器INITING NSWindowController創建:

@interface AppName_AppDelegate : NSObject 
    NSMutableArray *globalStatusArray; 

    @implementation AppName_AppDelegate 

    if (summaryWindow) { 
      [summaryWindow release]; 
     } // end if 
     summaryWindow = [[SummaryWindowController alloc] initWithWindowNibName:@"SummaryWindow" owner:globalStatusController]; 
    [summaryWindow showWindow:self]; 
    [summaryWindow setGlobalStatusArray:globalStatusArray]; 

,另一種是當筆尖負荷,相同的,但不同的對象,但是我現在不認爲創建那是因爲我沒有看到重複的NSLogs,所以我認爲它只是一些基本的內存問題與NSMutableArray(s)?

回答

1

當您應該保留aBuffer時,您正在保留myArray

+0

我只是保留myArray,因爲它是另一個問題的解決方案,我試着添加[aBuffer retain]; \t \t [myArray retain]; – acidprime

+0

另外我不認爲技術上我應該保留,因爲我使用initWithArray。 – acidprime

0

嘗試在你的界面使用

@property (nonatomic, retain) NSMutableArray *aBuffer 

接下來,

@synthesize aBuffer 

在您的.m文件。在此之後,

當您設置陣列,像這樣做:

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

注意「自我」,讓您指定的屬性。

+0

我已經試過把它作爲一個屬性,不去,做了上面的例子只是爲了確保它仍然不是初始化。我想知道如果我需要移動alloc/init它的init重寫方法,並使用其他一些保留的NSMUtableArray方法來複制數組 – acidprime