取消引用空指针,但我没有使用指针

发布于 2024-09-12 19:43:22 字数 826 浏览 3 评论 0原文

我在 xCode 中进行了“构建和分析”,并在我的 init 方法中将普通 int 设置为 0 时得到“取消引用空指针”。我在下面的代码中指出了我收到消息的行。我正在为 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

不是完整的代码,粘贴了我认为有用的内容。

由于我没有使用指针,所以我觉得这很奇怪。我怎么会收到这个消息?

谢谢和问候, 尼克拉斯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温折酒 2024-09-19 19:43:22

init 方法中的第一个 if 语句用于检查 [super init] 是否返回 nil。 (从技术上讲,应该写成 if ((self = [super init])),新的 LLVM 编译器会警告您)。

静态分析器正在检查所有可能的代码路径,即使 [super init] 返回 nil 的情况也是如此。在这种情况下,您的 if 语句失败,并且 selfnil。如果 selfnil 则其实例变量不可访问。

要解决此问题,您需要将初始化与图像初始化一起放置在 if 语句内,然后在 if 语句外部return self

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.

╰つ倒转 2024-09-19 19:43:22

您已将其申报为财产吗?我不确定在这种情况下是否有必要,但您还没有创建访问器方法(尽管我认为您仍在直接设置实例变量...)

,即在您的头文件

@property int stepX;

和 .m 文件中,

@synthesize stepX;

这将允许您访问变量 self.stepX 和 self.stepY。
有时分析器会犯错误...我注意到它不能非常有效地处理 while 循环。不管怎样,看看如果你添加这些代码行并回复我会发生什么。

Have you declared it as a property? I'm not sure if it's necessary in this case but you haven't made accessor methods (although I think you are still setting the instance variable directly...)

i.e., in your header file,

@property int stepX;

and in your .m file,

@synthesize stepX;

This will allow you to access the variable as self.stepX and self.stepY.
Sometimes the analyzer makes mistakes though... I noticed that it doesn't deal with while loops very effectively. Anyway, see what happens if you add those lines of code and get back to me.

凉月流沐 2024-09-19 19:43:22

你的初始化方法是错误的。

它应该看起来像这样:

- (id)initWithImage:(UIImage *)img:(NSString*)clr
{
    if (self = [super init])  // NB, this line should give you a waring
    {  
        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;
}

我假设您收到的消息来自静态分析器。由于 stepX 是一个实例变量,因此该行

stepX = 0;

实际上是

self->stepX = 0;

-> 具有其正常 C 含义的简写。由于该行位于代码中 self 不为零的测试之外,因此静态分析器正在标记一个问题。

Your init method is wrong.

It should look like this:

- (id)initWithImage:(UIImage *)img:(NSString*)clr
{
    if (self = [super init])  // NB, this line should give you a waring
    {  
        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;
}

I assume the message you are getting is from the static analyser. As stepX is an instance variable, the line

stepX = 0;

is really shorthand for

self->stepX = 0;

where -> has its normal C meaning. As that line is outside the test that self is non nil in your code, the static analyser is flagging an issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文