Cocos-2dx devp on android...simple sprite function causes seg faults

Hello,

New to cocos devlopment, i get seg fault at a particular location. Is there a way to avoid this:

Code: HelloWorldScene.h
_#ifndef HELLOWORLD_SCENE_H
_#define HELLOWORLD_SCENE_H
_#include “cocos2d.h”
_#include “cocostudio/CocoStudio.h”
_#include “ui/CocosGUI.h”

USING_NS_CC;

class HelloWorld : public cocos2d::Layer
{
Sprite *sprite2;
int i;
public:

// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();

// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
void update();

// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);

//void leftfn(Sprite *sprite,int i);

};
_#endif // HELLOWORLD_SCENE_H

HelloWorldScene.cpp
_#include “HelloWorldScene.h”
_#include “cocostudio/CocoStudio.h”
_#include “ui/CocosGUI.h”
_#include “cocos2d.h”

USING_NS_CC;

using namespace cocostudio::timeline;
using namespace ui;
Scene* HelloWorld::createScene()
{
// ‘scene’ is an autorelease object
auto scene = Scene::create();

// 'layer' is an autorelease object
auto layer = HelloWorld::create();

// add layer as a child to scene
scene->addChild(layer);

// return the scene
return scene;

}

// on “init” you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}

auto rootNode = CSLoader::createNode("MainScene.csb");

addChild(rootNode);

auto *sprite1 = (Sprite *)rootNode->getChildByName("bgsprite");
auto *sprite2 = (Sprite *)rootNode->getChildByName("object1");

auto *reset = (Button *)rootNode->getChildByName("Button_1");
auto *left= (Button *)rootNode->getChildByName("Button_2");
auto *right = (Button *)rootNode->getChildByName("Button_3");

//left->setTitleText("This is left");

left->addTouchEventListener([&](Ref* sender, cocos2d::ui::Widget::TouchEventType type){
    switch (type)
    {
    case ui::Widget::TouchEventType::BEGAN:
        break;
    case ui::Widget::TouchEventType::ENDED:
        i++;
        //std::cout << "Button 1 clicked" << std::endl;
        break;
    default:
        break;
    }
});

//right->setTitleText("This is right");

right->addTouchEventListener([&](Ref* sender, cocos2d::ui::Widget::TouchEventType type){
    switch (type)
    {
    case ui::Widget::TouchEventType::BEGAN:
        break;
    case ui::Widget::TouchEventType::ENDED:
        i--;
        //std::cout << "Button 1 clicked" << std::endl;
        break;
    default:
        break;
    }
});

//reset->setTitleText("This is reset");

reset->addTouchEventListener([&](Ref* sender, cocos2d::ui::Widget::TouchEventType type){
    switch (type)
    {
    case ui::Widget::TouchEventType::BEGAN:
        break;
    case ui::Widget::TouchEventType::ENDED:
        i = 0;
        //std::cout << "Button 1 clicked" << std::endl;
        break;
    default:
        break;
    }
});

this->addChild(sprite1);
this->addChild(sprite2);
this->addChild(reset);
this->addChild(left);
this->addChild(right);    

update();

return true;

}

void HelloWorld::update()
{
sprite2->setScale(i); //seg fault at this location. if commented app runs normally
}

Thanks.

Hi, it’s because you saved pointer to local variable:

fix:

 sprite2 = (Sprite *)rootNode->getChildByName("object1");

And better to name members of class with first underscore symbol “_”. e.g. _sprite2
In this case you will find the issue easily.
Also better to initialize members of class directly in header:

Sprite *sprite2 = nullptr;
int i = 0;

First, the function should be HelloWorld::update(float dt)
Second, as @dimon4eg said -

you are declaring sprite2 but not using member variable.
and any auto variable is not accessible outside the function because of scope resolution.

Good Practise :
always define a pointer variable in header file assigned with nullptr.
cross check code where it crashed.

Hope that helps.
Happy Coding :smile:

Hi dimon4eg,

Thanks! The seg fault is solved; but the sprite I am trying to access has disappeared, it seems I cannot reference the objects created in Cocos Studio, both the functions getChildByName("…"); and getChildByTag(int); are not working in case of Sprites, but I can access buttons in this fashion.

Hi pabitrapadhy,

I have declared the variables I need in the header files, and as @dimon4eg said assigning as null/0 helps in case of seg faults…but clearly the functions getChildByName("…"); and getChildByTag(int); are not working in case of Sprites…maybe my case…

Tchao!

It seems your sprite is not child of root node.
getChildByName is not recursive, it finds child only in one level down.

@dimon4eg, i don’t know how but it got solved by itself…maybe its a bug or something…