返回枚举 ivar 会导致 EXC_BAD_ACCESS

发布于 2024-12-29 08:56:14 字数 1577 浏览 0 评论 0原文

我正在编写一个游戏,其中 MainGameplay 类通过将 ivar 设置为枚举值来跟踪轮到谁:

typedef enum {
  GAME_NOT_STARTED,
  PLAYER_1_TO_MOVE,
  PLAYER_2_TO_MOVE
} WhoseTurnIsIt;

然后 GameBoard 类检查移动尝试是否有效,并调用 turnEnded:(WhoseTurnIsIt)turn 或 MainGameplay.m 中的 reportTurnFailure:(WhoseTurnIsIt)turn

当我尝试在 MainGameplay 的接收方法中访问此返回值时,我会立即收到 EXC_BAD_ACCESS。看来我应该保留一些东西,但你不能保留枚举。在调试器中,值就在那里,所以我不明白哪些内容被错误访问。

这是在 GameBoard 中执行调用的代码:

-(void)buttonPressed:(id)sender {
  CCArray *kids = [[[CCDirector sharedDirector] runningScene] children];
  if (!mainScene) { // mainScene is an ivar on each the GameBoard's buttons.
    for (CCScene *s in kids) {
      // this looks crazy because the "main" scene is actually a controlling layer that has as a child the main gameplay layer:
      if ([s isKindOfClass:[ControlLayer class]]) {
        self->mainScene = (MainGameplay *)((ControlLayer *) s).gameLayer;
      }
    }
  }

  if (MOVE_NO_ERROR == [self checkMove:mainScene.turn]) {
    [self setMove:mainScene.turn];
    [mainScene turnEnded:mainScene.turn]; // This line and the next are the ones causing the EXC_BAD_ACCESS
  } else [mainScene reportTurnFailure:mainScene.turn]; // This line too.
}

编辑 mainScene 中的函数被调用如下:

-(void) reportTurnFailure:(WhoseTurnIsIt)_turn {
  NSLog(@"MainScene still valid"); // This line works fine
  NSLog(@"Bzzzzzt. Player %@, try again", _turn); // This line crashes BUT _turn shows up with a proper value in the debugger.
}

I'm writing a game where a MainGameplay class keeps track of whose turn it is by setting an ivar to an enum value:

typedef enum {
  GAME_NOT_STARTED,
  PLAYER_1_TO_MOVE,
  PLAYER_2_TO_MOVE
} WhoseTurnIsIt;

Then the GameBoard class checks whether a move attempt is valid, and calls either turnEnded:(WhoseTurnIsIt)turn or reportTurnFailure:(WhoseTurnIsIt)turn in MainGameplay.m.

I get EXC_BAD_ACCESS as soon as I try to access this returned value back in MainGameplay, in the receiving methods. Seems like I should retain something, but you can't retain an enum. In the debugger the values are there, so I don't understand what's being accessed improperly.

This is the code doing the calling in GameBoard:

-(void)buttonPressed:(id)sender {
  CCArray *kids = [[[CCDirector sharedDirector] runningScene] children];
  if (!mainScene) { // mainScene is an ivar on each the GameBoard's buttons.
    for (CCScene *s in kids) {
      // this looks crazy because the "main" scene is actually a controlling layer that has as a child the main gameplay layer:
      if ([s isKindOfClass:[ControlLayer class]]) {
        self->mainScene = (MainGameplay *)((ControlLayer *) s).gameLayer;
      }
    }
  }

  if (MOVE_NO_ERROR == [self checkMove:mainScene.turn]) {
    [self setMove:mainScene.turn];
    [mainScene turnEnded:mainScene.turn]; // This line and the next are the ones causing the EXC_BAD_ACCESS
  } else [mainScene reportTurnFailure:mainScene.turn]; // This line too.
}

EDIT Functions in mainScene being called go like this:

-(void) reportTurnFailure:(WhoseTurnIsIt)_turn {
  NSLog(@"MainScene still valid"); // This line works fine
  NSLog(@"Bzzzzzt. Player %@, try again", _turn); // This line crashes BUT _turn shows up with a proper value in the debugger.
}

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

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

发布评论

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

评论(1

冷月断魂刀 2025-01-05 08:56:14

_turn 不是对象,但 %@ 格式说明符表示参数是对象。请改用%i

-(void) reportTurnFailure:(WhoseTurnIsIt)_turn {
  NSLog(@"MainScene still valid"); // This line works fine
  NSLog(@"Bzzzzzt. Player %i, try again", _turn); // This line crashes BUT _turn shows up with a proper value in the debugger.
}

_turn is not an object, but the %@ format specifier says that the argument is an object. Use %i instead.

-(void) reportTurnFailure:(WhoseTurnIsIt)_turn {
  NSLog(@"MainScene still valid"); // This line works fine
  NSLog(@"Bzzzzzt. Player %i, try again", _turn); // This line crashes BUT _turn shows up with a proper value in the debugger.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文