设置 int 变量导致错误

Setting int variable causing error

我过去发过关于这个的帖子,但现在这让我很困惑。我有一个名为 _state 的变量。它应该跟踪 GameCenter 状态。当我做...

int BBGameCenter::GameCenterState(){
    return _state;
}

return_状态;正在抛出 exc_bad_access。我最初认为这是因为 _state 为 null 所以我一直以这种方式进行调试。然而,当我尝试...

int BBGameCenter::GameCenterState(){
    _state = 0;
    return _state;
}

我收到了相同的错误消息,但这次是在我设置变量的行上,而不是 return 语句。所以我现在完全糊涂了。为 int 设置变量会导致运行时错误 exc_bad_access 怎么办?完整的 GameCenter 代码如下。感谢您的帮助!

#import <GameKit/GameKit.h>
@interface BBGameCenterDelegate : NSObject{
}
@end
class BBGameCenter{

    static BBGameCenter *_gameCenter;

    int _state;

    BBGameCenterDelegate *_delegate;

    NSMutableArray *_achievements;

public:
    BBGameCenter();

    static BBGameCenter *GetGameCenter();

    bool GameCenterAvail();

    void StartGameCenter();
    int  GameCenterState();

    void ShowLeaderboard( String leaderboard_ID );
    void ReportScore( int score,String leaderboard_ID );

    void ShowAchievements();
    void ReportAchievement( float percent,String achievement_ID );
    float GetAchievementPercent( String id );

    /
    GKAchievement *FindAchievement( String id );

    void GameCenterViewControllerDidFinish( UIViewController *vc );
};
/
BBGameCenter *BBGameCenter::_gameCenter;
@implementation BBGameCenterDelegate
-(void)gameCenterViewControllerDidFinish:(GKGameCenterViewController*)vc{
    BBGameCenter::GetGameCenter()->GameCenterViewControllerDidFinish( vc );
}
-(void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController*)vc{
    BBGameCenter::GetGameCenter()->GameCenterViewControllerDidFinish( vc );
}
- (void)achievementViewControllerDidFinish:(GKAchievementViewController *)vc{
    BBGameCenter::GetGameCenter()->GameCenterViewControllerDidFinish( vc );
}
@end
BBGameCenter::BBGameCenter():_state(-1),_delegate(0),_achievements(0){
    if( !GameCenterAvail() ) return;
    _delegate=[[BBGameCenterDelegate alloc] init];
    _state=0;
}
BBGameCenter *BBGameCenter::GetGameCenter(){
    if( !_gameCenter ) _gameCenter=new BBGameCenter();
    return _gameCenter;
}
bool BBGameCenter::GameCenterAvail(){

    /
    Class gcClass=NSClassFromString( @"GKLocalPlayer" );

    /
    NSString *reqSysVer=@"4.1";
    NSString *currSysVer=[[UIDevice currentDevice] systemVersion];
    BOOL osVersionSupported=([currSysVer compare:reqSysVer options:NSNumericSearch]!=NSOrderedAscending);

    return (gcClass && osVersionSupported);
}
void BBGameCenter::StartGameCenter(){

    if( _state ) return;

    GKLocalPlayer *localPlayer=[GKLocalPlayer localPlayer];

    if( localPlayer ){
        _state=1;
        [localPlayer authenticateWithCompletionHandler:^(NSError *error){
            if( localPlayer.isAuthenticated ){
                [GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements,NSError *error){
                    if( achievements ){
                        _achievements=[[NSMutableArray alloc] init];
                        [_achievements addObjectsFromArray:achievements];
                        /
                         int n=[_achievements count];
                         for( int i=0;i<n;++i ){
                         GKAchievement *achievement=[_achievements objectAtIndex:i];
                         bbPrint( String("Achievement:")+String(achievement.identifier) );
                         }
                         */
                    }
                    _state=2;
                }];
            }else{
                _state=-1;
            }
        }];
    }else{
        _state=-1;
    }
}

此处出现错误vvv

int BBGameCenter::GameCenterState(){
    return _state;
    }
void BBGameCenter::ShowLeaderboard( String leaderboard_ID ){

    if( _state!=2 ) return;

    GKLeaderboardViewController *vc=[[GKLeaderboardViewController alloc] init];
    if( !vc ) return;

    vc.leaderboardDelegate=(id)_delegate;
    vc.timeScope=GKLeaderboardTimeScopeToday;
    vc.category=leaderboard_ID.ToNSString();

    _state=3;

    /
    /
    [BBIosGame::IosGame()->GetUIAppDelegate()->viewController presentModalViewController:vc animated:YES];
}
void BBGameCenter::ReportScore( int value,String leaderboard_ID ){

    if( _state!=2 ) return;

    GKScore *score=[[GKScore alloc] initWithCategory:leaderboard_ID.ToNSString()];

    score.value=value;
    score.context=0;

    [score reportScoreWithCompletionHandler:^(NSError *error){} ];
}
void BBGameCenter::ShowAchievements(){

    if( _state!=2 ) return;

    GKAchievementViewController *vc=[[GKAchievementViewController alloc] init];
    if( !vc ) return;

    vc.achievementDelegate=(id)_delegate;

    _state=4;

    /
    /
    [BBIosGame::IosGame()->GetUIAppDelegate()->viewController presentModalViewController:vc animated:YES];
}
GKAchievement *BBGameCenter::FindAchievement( String id ){
    if( !_achievements ) return 0;
    NSString *str=id.ToNSString();
    int n=[_achievements count];
    for( int i=0;i<n;++i ){
        GKAchievement *achievement=[_achievements objectAtIndex:i];
        if( [achievement.identifier isEqualToString:str] ) return achievement;
    }
    return 0;
}
void BBGameCenter::ReportAchievement( float percent,String achievement_ID ){

    if( _state!=2 ) return;

    GKAchievement *achievement=FindAchievement( achievement_ID );
    if( !achievement ){
        achievement=[[GKAchievement alloc] initWithIdentifier:achievement_ID.ToNSString()];
        [_achievements addObject:achievement];
    }

    achievement.percentComplete=percent;
    achievement.showsCompletionBanner=true;

    [achievement reportAchievementWithCompletionHandler:^(NSError *error){} ];
}
float BBGameCenter::GetAchievementPercent( String achievement_ID ){

    GKAchievement *achievement=FindAchievement( achievement_ID );
    if( !achievement ) return 0;

    return achievement.percentComplete;
}
void BBGameCenter::GameCenterViewControllerDidFinish( UIViewController *vc ){

    _state=2;

   /
    /
    [BBIosGame::IosGame()->GetUIAppDelegate()->viewController dismissModalViewControllerAnimated:YES];
}

我缺少头文件,但我认为我看到了问题所在。我假设您正在定义 Objective C 属性 并尝试从 C++ class.

访问它

从这里开始,一切都是猜测,您真的应该放弃 C++ 或至少拆分它。 C++ 看到行

@property int _state

...看到全局变量就觉得还行。但是当你编译程序时,它在 class 中,你还没有初始化它或通过

之类的方式访问它
bbgcDelegate.state

因此,当 C++ 代码尝试访问变量时,那里什么也没有,因此出现错误。本质上,您以某种方式欺骗 IDE 接受此代码,但它不应该编译。