2012-07-24 2 views
0

Aaron Hillegass의 Mac OSX 용 Cocoa Programming 8 장에서이 프로그램을 실행할 때 오류가 발생합니다. 이 프로그램은 tableview를 어레이 컨트롤러에 바인드합니다. 어레이 컨트롤러의 setEmployees 방법,왜 이러한 메모리 문을 추가해야합니까?

-(void)setEmployees:(NSMutableArray *)a 
{ 
    if(a==employees) 
     return; 
    [a retain];//must add 
    [employees release]; //must add 
    employees=a; 
} 
책에서

, 두에서 유지하고 나는 새로운 직원을 추가하려고 할 때마다 포함되지 않은 문장 내 프로그램이 충돌을 놓습니다. 인터넷 검색 후 프로그램 충돌을 막기 위해 필자는이 두 가지 필수 선언문을 발견했습니다. 여기서 메모리 관리를 이해할 수 없습니다. aemployees에게 할당하고 있습니다. 내가 할당을 해제하지 않으면 왜 a을 유지해야합니까? 왜 마지막 할당 문에서 사용하기 전에 employees을 출시 할 수 있습니까?

답변

2

MRC (Manual Reference Counting)를 사용하는 설정 도구의 표준 패턴입니다.

-(void)setEmployees:(NSMutableArray *)a 
    { 
     if(a==employees) 
      return;   // The incoming value is the same as the current value, no need to do anything. 
     employees=a;   // Set the pointer to the incoming value 
    } 

(가) 유지/릴리스 당신을 위해 수행됩니다

자동 참조에서
-(void)setEmployees:(NSMutableArray *)a 
{ 
    if(a==employees) 
     return;   // The incoming value is the same as the current value, no need to do anything. 
    [a retain];   // Retain the incoming value since we are taking ownership of it 
    [employees release]; // Release the current value since we no longer want ownership of it 
    employees=a;   // Set the pointer to the incoming value 
} 

계산 (ARC) 접근을 단순화 할 수 있습니다 단계적으로, 이것은 무엇이다. 어떤 종류의 충돌이 발생했는지에 대해서는 언급하지 않았지만 MRC 프로젝트에서 ARC 샘플 코드를 사용하고있는 것처럼 보입니다.

+0

당신 말이 맞습니다. 4.1을 사용 중이며 ARC가 없습니다. 감사. – Standstill

관련 문제