0
我現在這是一個愚蠢的問題,但我仍然對這種情況有一些瞭解甚少。這是關於內存管理和引用計數,我有一些疑問,如果我使用副本,分配和可變副本,有多少引用計數會增加。這是我的代碼有:多少引用計數增加
這是myController.h:
#import <UIKit/UIKit.h>
@interface myController : UIViewController {
NSMutableString *mutableStringCode;
}
@property (nonatomic, copy) NSMutableString *mutableStringCode;
@end
,這是myController.m
#import "myController.h"
@implementation myController
-(void)viewDidLoad{
mutableStringCode = [[NSMutableStringCode alloc]init];
[self refresh];
}
-(void)refresh{
NSMutableString *myFileContents = [NSMutableString stringWithContentsOfFile:localPath encoding:NSUTF8StringEncoding error:&error];
mutableStringCode = [myFileContents mutableCopy];
//another code
myFileContents = nil;
}
-(void)dealloc{
[mutableStringCode release];
[super dealloc];
}
@end
在這段代碼
,我有些疑惑: 1.如何那個mutableStringCode中引用計數增加了很多? 2.是否使用copy
而不是retain
設置mutableStringCode
屬性的真實方法? 3.我是否需要在設置屬性後複製mutableStringCode
?
可以有些身體描述給我? 謝謝
謝謝你的回答,我盡力去做,但我想問一下,我是否必須在該viewDidload中釋放mutableStringCode,還有在dealloc方法中? – 2011-04-24 11:44:21
將值分配給mutableStringCode之前,請確保完全釋放當前值。建議的操作是在將myFileContents分配給它之前,將自動釋放消息發送到mutableStringCode。但是,如果您不明白爲什麼,請仔細閱讀並仔細閱讀有關Objective-C內存管理的內容(在StackOverflow或其他地方)。這是一件非常重要的事情。 – bealex 2011-04-24 16:45:52