어제까지 내가 iPhones 메모리 관리를 이해했다고 생각했습니다. 그럼 여기 내 문제 : 나는 예를"self.variable = value"로 인스턴스 변수를 두 번 설정하면 메모리 누수가 발생합니까?
self.dicParams = dicValues;
의의 ViewController의 할당 한 후, 다른 곳에서 설정하면 그것은 누출로 바뀝니다 ...
// .h file
@property(nonatomic, retain) NSMutableDictionary *dicParams;
@property(nonatomic, retain) NSMutableDictionary *dicReferences;
@property(nonatomic, retain) FtMonitorHandler *monitorHandler;
// .m file
@synthesize dicParams, dicReferences, monitorHandler;
- (id)init {
self = [super init];
if (self) {
self.dicParams = [[NSMutableDictionary alloc] init];
self.dicReferences = [[NSMutableDictionary alloc] init];
self.monitorHandler = [[FtMonitorHandlerService alloc] init];
}
return self;
}
- (void)dealloc {
[monitorHandler release];
[dicParams release];
[dicReferences release];
[super dealloc];
}
인스턴스 변수를 설정하는 나의 이해 "self ... ..."는 현재 값이 "release"되고 "retain"으로 설정된다는 것입니다.
악기를 조금 사용해 보았습니다. 결과 :
내가 놓친 것이 있습니까? 그렇지 않으면 정상적으로 작동합니까?
수정 : 제안 된 변경 사항을 구현하려고했습니다. 내 변수가 GUI 요소가 아니기 때문에 오래 사용할 수 있습니다. (UIView의, UILabel의 등)
오토 릴리즈는
- (void)loadView {
[super loadView];
// ... here is some other stuff ...
self.lblDeparture = [[[UILabel alloc] init] autorelease];
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
self.lblDeparture = nil;
}
- (void)dealloc {
[lblDeparture release];
[super dealloc];
}
내가 아주 확실하지 않다 경고 메모리 후 응용 프로그램 충돌이 발생할 것입니다,하지만 난 다음 줄은 진짜 문제가 있다고 가정 :
를CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, INFO_VIEW_HEIGHT);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
[imageView addSubview:lblDeparture];
[lblDeparture release]; // is this correct?
[self.view addSubview:imageView];
[imageView release];
왜 self.yourObjects를 사용하는지, 동일한 클래스에 있으므로 self.yourObject를 호출하여 보유하지 않고 자기없이 시도 할 수 있습니다. – Ravin
첫 번째 참조가 사라질 것이기 때문에 "variable = ..."변수를 두 번 할당하면 누수가 발생하지 않을까요? "self.variable ..."이 없다면 – user707342
아니오 dicParams = [[NSMutableDictionary alloc] init]으로 시도해야한다고 말합니다. 대신 self.dicParams = [[NSMutableDictionary alloc] init]; . – Ravin