0

更新帖子

现在,我在 7 次中有 2 次主要有 EXC_BAD_ACCESS,我不知道为什么当 pumpViewHeight 的结果为 607 时,pumpCustomView 类的 heightOfPumpView 结果为 0。

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

泵模型.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];
}
4

2 回答 2

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 ; 
}
于 2012-04-16T20:12:45.107 回答
0

如果发生崩溃,请发布崩溃的回溯。

看你的setNumberOfPump:方法,似乎很不对劲。

  • 标签被分配,然后被释放,可能将实例变量作为悬空引用,稍后会崩溃

  • labelsArray被泄露

  • dealloc不会释放任何记忆

您应该尝试在您的代码上运行构建和分析,修复任何错误。上述问题与有关init模式的评论相结合,表明您可能应该查看 Objective-C 文档以更好地理解初始化和内存管理模式。

于 2012-04-16T20:49:32.350 回答