设置int变量导致错误

时间:2016-01-07 21:31:29

标签: ios debugging variables runtime-error objective-c++

我过去发过这个帖子,但现在这让我很困惑。我有一个名为_state的变量。假设要跟踪GameCenter状态。我什么时候......

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    NSLog(@"TOUCHED");

    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self];
    vector_float3 touchVector = {touchLocation.x, touchLocation.y, 0.5};
    SCNVector3 unprojectedVector = SCNVector3FromFloat3([self unprojectPoint:touchVector]);

    NSArray *result = [self hitTest:CGPointMake(unprojectedVector.x, unprojectedVector.y) options:NULL];

    if([result count] > 0) {
        SCNHitTestResult *hitResult = [result firstObject];
        [hitResult.node removeFromParentNode];
    }
}

return _state;抛出一个exc_bad_access。我原本以为这是因为_state是null所以我一直在以这种方式调试。但是,当我尝试......

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

我收到了相同的错误消息,但这一次是在我设置变量而不是返回语句的行上。所以我现在非常困惑。如何为int设置变量会导致运行时错误exc_bad_access?完整的GameCenter代码如下。谢谢你的帮助!

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

此处发生错误vvv

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

1 个答案:

答案 0 :(得分:0)

我错过了头文件,但我认为我发现了问题。我假设您正在定义Objective C属性并尝试从C ++类访问它。

从现在开始,这一切都是猜测,你应该放弃C ++或者至少拆分它。 C ++看到了行

@property int _state

......并认为它看起来像一个全局变量。但是当你编译程序时,它是在一个类中,你没有初始化它或通过做类似

的方式访问它
bbgcDelegate.state

因此,当C ++代码试图访问变量时,那里什么都没有,因此错误。实质上,不知何故,你欺骗IDE接受这个代码,但它不应该编译。