2012-04-16 7 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