Label crashes app in android when switching scenes

I have a weird bug where switching scenes causes my app to crash in android. It works fine in windows and only happens in android when I add labels. When the app opens the labels load fine but then when I change scenes the app eventually crashes, sometimes on the first scene transition and sometimes in the second transition going back to the first scene. The app is basically two scenes each with a button and a label, the button transitions between the scenes. The code for the two scenes is shown below:

HelloWorldScene.cpp:

 #include "HelloWorldScene.h"
 #include "AnotherScene.h"
    
    Scene* HelloWorldScene::createScene()
{
    auto scene = Scene::create();
    auto layer = HelloWorldScene::create();
    scene->addChild(layer);
    return scene;
}

bool HelloWorldScene::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
	
	auto label = Label::createWithTTF("Hello Scene", "fonts/arial.ttf", 30);
	label->setPosition(Point(500, 500));
	this->addChild(label);
    
	ui::Button* switchSceneButton = ui::Button::create("CloseNormal.png", "CloseSelected.png");
	switchSceneButton->setPosition(Point(700, 500));
	switchSceneButton->addTouchEventListener(CC_CALLBACK_2(HelloWorldScene::switchScene, this));
	this->addChild(switchSceneButton);

	return true;
}

void HelloWorldScene::switchScene(cocos2d::Ref* sender, ui::Widget::TouchEventType type) {
	auto scene = AnotherScene::createScene();
	Director::getInstance()->replaceScene(TransitionFade::create(1, scene));
}

AnotherScene.cpp:

#include "AnotherScene.h"
#include "HelloWorldScene.h"

Scene* AnotherScene::createScene()
{
    auto scene = Scene::create();
    auto layer = AnotherScene::create();
    scene->addChild(layer);
    return scene;
}

bool AnotherScene::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
	
	auto label = Label::createWithTTF("Another Scene", "fonts/arial.ttf", 30);
	label->setPosition(Point(500, 500));
	this->addChild(label);

	ui::Button* switchSceneButton = ui::Button::create("CloseNormal.png", "CloseSelected.png");
	switchSceneButton->setPosition(Point(700, 500));
	switchSceneButton->addTouchEventListener(CC_CALLBACK_2(AnotherScene::switchScene, this));
	this->addChild(switchSceneButton);
    
    return true;
}

void AnotherScene::switchScene(cocos2d::Ref* sender, ui::Widget::TouchEventType type) {
	auto scene = HelloWorldScene::createScene();
	Director::getInstance()->replaceScene(TransitionFade::create(1, scene));
}

The font is saved in my resources folder under fonts/arial.ttf. Android studio also displays the fonts under the assets directory so I’m pretty sure the files are getting found. The error message that comes out in android studio is: Fatal signal 11 (SIGSEGV), code 2, fault addr 0x43c8c02c in tid 11723 (GLThread 44794). Also I’m using a Samsung S4 to test. Any ideas?

EDIT: it also crashes for me when using a Nexus 5, so I’m pretty sure the error is not device specific. Also I’m using cocos2d-x version 3.12.

Hi
I will recommend you to add this switch case in your switchScene event Listener as according to me it will try to change scene if not 3 atleast 2 times during the diffrent began and ended event calls try this and it should work rest of the code looks fine.

switch (type)
{
case ui::Widget::TouchEventType::BEGAN:
break;
case ui::Widget::TouchEventType::ENDED:
{
auto scene = HelloWorldScene::createScene();
Director::getInstance()->replaceScene(TransitionFade::create(1, scene));
}
break;
default:
break;
}

nope, just tried it, still crashes. I don’t think the scene transition is getting called twice since the program runs fine if I take out the labels. It only crashes when the labels are there.

So has anyone been able to look at this? Has anyone at least replicated the problem? This seems like a rather critical issue of which someone has had to run into. Because at this point I seriously don’t know how to progress. I tried using BMfonts as well but got a similar error on android. Anyone?

Hey hotcoco, I am facing the same issue. It’s really irritating at this stage of development.
Did you find any solution or walk through around it ?

I couldn’t find a solution but it was version related because it never occurred in 3.10 so I stuck with that older version.

It is possible that your label has too big size. So it does not fit inside texture.
Go to CCFontAtlas.cpp and find these lines:

const int FontAtlas::CacheTextureWidth = 512;
const int FontAtlas::CacheTextureHeight = 512;

Try some bigger values:

const int FontAtlas::CacheTextureWidth = 2048;
const int FontAtlas::CacheTextureHeight = 2048;