2012-04-16 66 views
0

更新後通過實例變量EXC_BAD_ACCESS的主要功能和無法通過類

現在,我對主2次EXC_BAD_ACCESS出7,我不知道爲什麼heightOfPumpView結果從0所述pumpCustomView類時pumpViewHeight的結果爲607

PumpViewController.m

#import "PumpViewController.h" 
#import "PumpModel.h" 
#import "PumpCustomView.h" 

@implementation PumpViewController 

@synthesize labels; 
@synthesize heightOfPumpView; 
- (id)init 
{ 

if (self = [super init]) 
{ 
    labels = [[PumpModel alloc]init]; 

    PumpCustomView* pumpView = [PumpCustomView alloc]; 
    heightOfPumpView = [pumpView pumpViewHeight]; 
    [labels pumpCreateLabel:heightOfPumpView]; 
    labelsArray = [[NSMutableArray alloc]initWithArray:[labels labelsGroup]]; 

    [labels release]; 

     if (labelsArray!=nil) 
     { 
      [pumpView addSubview:[labelsArray objectAtIndex:2]]; 
     } 



    [labelsArray release]; 
    [pumpView release]; 
} 

return self; 
} 


-(void) dealloc 
{ 
[super dealloc]; 

} 

@end 

PumpModel.m

#import "PumpModel.h" 
#import "PumpViewController.h" 
#import "PumpCustomView.h" 

@implementation PumpModel 
@synthesize labelsGroup; 

-(id)init 
{ 
self = [super init]; 
return self; 
} 

-(void)pumpCreateLabel:(float)pumpViewHeight 
{ 
theNumberOfPump = 8; 
PumpViewController* pumpViewControllerAlloc = [PumpViewController alloc]; 
labelsGroup = [[NSMutableArray alloc]init]; 

for (int i = 0;i < theNumberOfPump; i++) 
{ 
    int pumpViewHeight = [pumpViewControllerAlloc heightOfPumpView]; 
    int pumpViewWidthA = 259; 

    int resultHeight = pumpViewHeight/theNumberOfPump; 
    CGFloat resultWidth = pumpViewWidthA/2; 
    positionChart[i] = resultHeight * i;   

    newLabel[i] = [[NSTextField alloc] init] ; 

    [newLabel[i] setIntValue:i]; 

    newLabel[i].frame = CGRectMake(resultWidth, positionChart[i], 300, 100); 
    newLabel[i].font= [NSFont fontWithName:@"Arial" size:12]; 
    newLabel[i].textColor= [NSColor blackColor]; 
    newLabel[i].backgroundColor= [NSColor whiteColor]; 

    [labelsGroup addObject:newLabel[i]]; 
    [newLabel[i] release]; 

    NSLog(@"%@ %d",[[labelsGroup objectAtIndex:i] stringValue],positionChart[i]); 
} 
[pumpViewControllerAlloc release]; 

} 

-(void) dealloc 
{  
[labelsGroup release]; 
[super dealloc]; 
} 

回答

3

你不應該[super init]之前發送消息的對象,如:

- (id)init 
{ 
    if (self = [super init]) 
    { 
     [self setNumberOfPump:8]; 
    } 
    return self; 
} 

這也適用於:

-(id)initWithNumberOfPump:(int)numberOfPump 
{ 
    if (self = [super init]) { 
     theNumberOfPump = numberOfPump; 
     [self pumpCreateLabel]; 
    } 
    return self ; 
} 
+0

我同意,但即使我將它放入if函數,它也會執行與前面所述相同的操作。我在[self setNumberOfPump:8]上得到了一個EXC_BAD_ACCESS; – 2012-04-16 20:17:55

+0

這次我從PumpModel文件 – 2012-04-16 20:24:57

+0

上得到[self pumpCreateLabel]上的exc_bad_access你見過我的編輯嗎?你是否修復過'initWithNumberOfPump:'? – MByD 2012-04-16 20:26:29

0

如果你有一個碰撞,後的回溯崩潰。

看着你的setNumberOfPump:方法,這看起來很不對。

  • 標籤分配,然後釋放,有可能留下的實例變量作爲懸掛引用,會崩潰後

  • labelsArray泄漏

  • dealloc不會釋放任何內存

您應該嘗試在代碼上運行Build and Analyze,修復任何錯誤。上述問題與有關init模式的意見相結合表明,您應該查看Objective-C文檔以更好地理解初始化和內存管理模式。

+0

我已盡了最大努力來糾正這些問題。現在它編譯正確的3次。當它不編譯時,xcode在main上引發一個exc_bad_access。我試圖使用NSZombie和泄漏工具來解決它,但沒有什麼特別的事情發生。我更新了我的帖子。謝謝你的幫助! – 2012-04-25 11:21:18