2011-08-24 3 views
2

나는 pickerView가 있지만 시뮬레이터에 나타나지만 시도하고 스크롤하면 main.m 파일에서 EXC_BAD_ACCESS가 충돌합니다.이동했을 때 PickerView가 이동했을 때 EXC_BAD_ACCESS가 충돌 함

제가 pList에서로드하는 배열과 관련이 있다는 것을 알고 있습니다. 프로그램에서 초기화 된 배열로 이것을 시도하면 (주석 처리 된 섹션 참조), 'nil 배열 끝에? 그렇다면 어떻게 볼 수 있으며 어떻게 추가 할 수 있습니까? 나는 내 머리 밖으로의 왼쪽 무슨 당기는거야, 그리고 당신은 객체 (들)을 보유하고 있지 않습니다 ...

// Method to define the numberOfComponent in a picker view. 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
return 2; 
} 


// Method to define the numberOfRows in a component using the array. 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent :(NSInteger)component 
{ 
if (component==0) 
{ 
    return [maximumSpeed count]; 
} 
else 
{ 
    return[warningTime count]; 
} 

} 

//PickerViewController.m 
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 

    NSString *stringToReturn = nil; 

    switch (component) 
    { 
     case 0: 
      stringToReturn = [maximumSpeed objectAtIndex:row]; 
      break; 
     case 1: 
      stringToReturn = [warningTime objectAtIndex:row]; 
      break; 
    } 
return stringToReturn; 
} 




// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

// finding the path and loading the pList as a dictionary into 'data' 
NSString *pListFile = [[NSBundle mainBundle] pathForResource:@"PropertyList" ofType:@"plist"]; 
data =[NSDictionary dictionaryWithContentsOfFile:pListFile]; 

// get and set up the 2 arrays 
maximumSpeed = [data valueForKey:@"MaximumSpeed"]; 
warningTime = [data valueForKey:@"WarningTime"]; 


//maximumSpeed =[[NSArray alloc] initWithObjects:@"Running",@"Crying",@"Boring",@"Working",nil]; 
//warningTime = [[NSArray alloc] initWithObjects: @"Happy", @"Sad" , @"Good", @"joyce",nil]; 

NSLog(@"%@", maximumSpeed); 

NSLog(@"%@", warningTime); 

} 

(void)viewDidUnload 
{ 
[self setTxt1:nil]; 
[pickerView release]; 
pickerView = nil; 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
} 



- (void)dealloc { 
[txt1 release]; 
[pickerView release]; 
[warningTime release]; 
[maximumSpeed release]; 
[super dealloc]; 
} 

답변

5

을 이것에 비교적 새로운 해요 반환하십시오

나는 이것에 어떤 도움을 주셔서 감사합니다 valueForKey에 의해,이 경우 귀하의 배열. 이 객체는 해제되고 당신은 매달려있는 포인터로 끝납니다. 따라서 충돌. 문제는 여기에 있습니다 :

maximumSpeed = [data valueForKey:@"MaximumSpeed"]; 
warningTime = [data valueForKey:@"WarningTime"]; 

당신은 그들을 지켜야합니다.

maximumSpeed = [[data valueForKey:@"MaximumSpeed"] retain]; 
warningTime = [[data valueForKey:@"WarningTime"] retain]; 

작성한 개체를 소유하고 있기 때문에 다음이 작동합니다.

//maximumSpeed =[[NSArray alloc] initWithObjects:@"Running",@"Crying",@"Boring",@"Working",nil]; 
//warningTime = [[NSArray alloc] initWithObjects: @"Happy", @"Sad" , @"Good", @"joyce",nil]; 

이것은 또한 dictionaryWithContentsOfFile에 적용

data =[NSDictionary dictionaryWithContentsOfFile:pListFile]; 

당신은 유지 (이전처럼) 또는 alloc-init 버전으로 전환 할 수 있습니다

data = [[NSDictionary dictionaryWithContentsOfFile:pListFile] retain]; 
// or 
data = [[NSDictionary alloc] initWithContentsOfFile:pListFile]; 

당신은 객체를 보유하고 있지 않습니다 메서드 이름이 "alloc", "new", "copy"또는 "mutableCopy"로 시작하지 않는 한 메서드에서 반환됩니다.

+0

대단히 감사합니다. 답변에 시간을 내 주셔서 감사 드리며 효과가있었습니다. 그것은 단지 이렇게 나는이 게시물 http://stackoverflow.com/questions/2426651/pickerview-exc-bad-access-을 읽고 있었는데 - 이것은 같은 문제입니다. 인터페이스에서 이러한 변수를 선언했지만 전역 변수가 아니라고 생각합니다. – John67

+0

@ John67 당신은 환영합니다 :) – albertamg

0

이미 말했듯이 객체 (배열) maximumSpeed ​​및 warningTime을 복사하거나이를 유지하고 해제 할 수 있습니다.

발표하는 중 ... 당신은 pickerView를 두 번 내 보내고 있습니다. 첫 번째는 viewDidUnload이고 두 번째는 dealloc입니다. 첫 번째 릴리스 이후에 nil을 지정하기 때문에 실제로 충돌없이 작동 할 수 있으므로 두 번째 릴리스는 어쨌든 충돌없이 처리되어야합니다.

그러나 이는 특히 메모리 유지 관리에주의해야한다는 것을 나타냅니다.

왜 instrumemts 응용 프로그램에서 좀비를 추적하지 않습니까? xcode4에서 cmd-i로 시작합니다. 대부분의 경우 누락 된 보관 항목 (또는 사본)에 대한 자세한 정보를 얻을 수있는 유용한 힌트를 제공합니다.

+0

감사합니다 헤르만, 메모리 관리는 제가 집중해야 할 부분입니다 - 좀비 앱을 시험해 보았습니다. 그것은 변수의 이름을 말할 수 있다면 유용 할 것입니다. – John67

+0

John, 변수 만 알려주는 것이 아닙니다. 그것은 alloc/retain/release history에 대한 상세한 뷰를 제공합니다. 이것은 분석을위한 훌륭한 도구 일뿐만 아니라,그것은 모든 autorelease 메커니즘을 이해하는 데 도움이됩니다. 일부 지침은 Google에 문의하십시오. –

관련 문제