tags:

views:

155

answers:

1

Whenever I call the doSomething function my program crashes. The actual code as a bit of array bounds checking, so I know that's not an issue.

myClass.h

#import <Foundation/Foundation.h>
@interface myClass : NSObject {
    BOOL **myMatrix;

}
@property(readwrite) BOOL **myMatrix;

-(myClass*)initWithWidth: (int)w andHeight: (int)h;
-(void)doSomething;
+(BOOL **) createMatrixWithHeight: (int)h andWidth: (int)w;

@end

myClass.m

#import "myClass.h"
#import <Foundation/Foundation.h>

@implementation myClass

@synthesize myMatrix;

-(myClass*)initWithWidth: (int)w andHeight: (int)h    {
    self = [super init];
    myMatrix = [myClass createMatrixWithHeight: h andWidth: w];
    return self;
}

-(void)doSomething{
    myMatrix[2][2] = YES;
}

+(BOOL **) createMatrixWithHeight: (int)h andWidth: (int)w{
    BOOL **newMatrix;

    newMatrix = malloc(w * sizeof(BOOL *));

    int i;
    for(i = 0; i < w; i++){
     newMatrix[i] = malloc(h * sizeof(BOOL));
    }

    return newMatrix;
}
@end
A: 

There must be some important difference between the code you posted and the code in your program, because I copied and pasted this in to a program and it ran fine without crashing.

So the answer to your question is this: just like in the code you posted.

Jim Puls