vlad2048
vlad2048

Reputation: 13

Very strange Objective C error

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;
}

Upvotes: 1

Views: 89

Answers (1)

Marcelo Cantos
Marcelo Cantos

Reputation: 185862

The reference to mode is shorthand for self->mode, which induces the block to retain the class instance.

Upvotes: 8

Related Questions