Android Cocos2d-x: fatal signal 11 by referencing class instance variable

 GAME.CPP
   Utils::hudLayer()->didScore();


  HUD.CPP
  class HUD:public CCLayer
  {
     int score;
     public:
        void didScore();
  };

  void HUD::didScore() {
    score++;            //<------------ this line crashes the app ////////////////
    CCLog("score: %d", score);
    scoreLabel->setString(
            CCString::createWithFormat("%d", score)->getCString());

  }


  UTILS.CPP
  HUD* Utils::hudLayer() {
    return (HUD *) Utils::layerWithTag(TAG_HUD);
}

CCLayer* Utils::layerWithTag(int tag) {
    CCScene *sc = CCDirector::sharedDirector()->getRunningScene();
    if (sc->getTag() == TAG_GAME_SCENE) {
        CCLayer *layer = (CCLayer*) sc->getChildByTag(tag);
        return layer;
    }
    return NULL;
}

Try initializing score (to 0 for example).

You may be calling didScore() method on NULL if the hudLayer() call wasn’t able to find the game scene or the hud layer by tag. Try:

auto hud = Utils::hudLayer();
if(hud) {
   hud->didScore();
} else {
   CCLOG("failed to find hudlayer!");
}