I did the "Build and analyze" in xCode and get "Dereference of null pointer" when setting a normal int to 0 in my init-method. I noted in my code below for which row I get the message. I'm developing for iPhone.
Bric.m
#import "Bric.h"
@implementation Bric
- (id)initWithImage:(UIImage *)img:(NSString*)clr{
if (self = [super init]) {
image = [[UIImageView alloc] initWithImage:img];
}
stepX = 0; //It's for this line I get the message
stepY = 0;
oldX = 0;
color = [[NSString alloc]initWithString:clr];
visible = YES;
copied = NO;
return self;
}
@end
Bric.h
#import <Foundation/Foundation.h>
@interface Bric : NSObject {
int stepX;
int stepY;
}
-(id)initWithImage:(UIImage *)img:(NSString *)clr;
@end
It's not the complete code, pasted what I think is useful.
Since I am not using a pointer I find this quite strange. How come I get this message?
Thanks and Regards, Niklas
The first if
statement in your init method is checking whether or not [super init]
returns nil
. (Technically it should be written if ((self = [super init]))
, which the new LLVM compiler will warn you about).
The static analyser is checking ALL possible code paths, even the case where [super init]
returns nil. In this case, your if
statement fails and self
is nil
. If self
is nil
then its instance variables aren't accessible.
To fix this, you need to place your initialisations inside the if
statement with the image initialisation and then return self
outside the if statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With