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

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];
}
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 に答える