非常奇怪的 Objective C 错误
我想说我非常了解 Objective C。但我刚刚发现了一个非常奇怪的错误。 我有一个“int模式”;类中的成员变量。只写“模式”;在 init 方法中改变类的行为(稍后不会被释放) 即使编译器给我警告“语句无效”,
这是怎么回事? 我可以不确定对象何时被释放吗?
这是我的代码,更详细一些:
@interface HelpScene : CCScene {
int mode;
}
以及在实现中
- (id) init {
if (self=[super init]) {
[[SomeObject alloc] initWithBlock:^(id sender) {
mode; // CHANGES BEHAVIOUR
[Call CCDirector.replaceScene which usually ends up deallocing self
(the current scene). But not with the previous line anymore]
}
}
return self;
}
I'd say I know Objective C pretty well. But I've just tracked down a bug to something very strange.
I have an "int mode;" member variable in a class. And just writing "mode;" in the init method changes the behavior of the class (it doesn't get dealloced later on)
Even though the compiler gives me the warning "Statement has no effect"
What is going on ?
Can I not be sure of when an object is dealloced ?
Here's my code in a bit more detail:
@interface HelpScene : CCScene {
int mode;
}
and in the implementation
- (id) init {
if (self=[super init]) {
[[SomeObject alloc] initWithBlock:^(id sender) {
mode; // CHANGES BEHAVIOUR
[Call CCDirector.replaceScene which usually ends up deallocing self
(the current scene). But not with the previous line anymore]
}
}
return self;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对
mode
的引用是self->mode
的简写,它导致块保留类实例。The reference to
mode
is shorthand forself->mode
, which induces the block to retain the class instance.