Pointer to control of loaded scene (.csb)

Hi,
I’m new to cocos2d-x and i can’t understand why my control is deleted after execution of init() that contains loading of scene.
To load scene from .csb i use method described there: Import Scene From CocoStudio into code
I tried to use

loadedScene->getChildByName(“myControl”);
// or even
[MyScene::init()] getChildByName(“Scene”)->getChildByName(“myControl”);

but every time when it handle click event, the pointer to the control shows 0xFEEEFEEE.

In other words:

bool MyScene::init()
{

auto scene = loadScene(“Scenes/scene.csb”);
this->addChild(scene);
myObject = new MyObject(scene->getChildByName(“myControl”));
// myObject = new MyObject(getChildByName(“Scene”)->getChildByName(“myControl”));
}

MyObject::MyObject(Node* image)
: image(static_cast < ui::ImageView* > (image))
{
// this->image and image seems ok
autorelease();
this->image->setTouchEnabled(true);
this->image->addClickEventListener(CC_CALLBACK_1(MyObject::imageClicked, this));
}

void MyObject::imageClicked(Ref* sender)
{
// the image pointer is pointing to 0xFEEEFEEE
// sender argument only partially solve my problem
}

Did I miss something?

I’m not completely sure, but my guess would be because you call autorelease() inside MyObject but you never retain it, it is getting released and the memory is freed.

If you comment out the line autorelease();, does it work?

Yes, it does. lol
I don’t understand why Ref requires to increase _referenceCount if constructor set it to 1 and 1 is a correct value.
It’s not intuitive for me, after working on std::shared_ptr.
Anyway thx for help.

I haven’t used shared_ptr so I can’t compare, but I think autorelease decreases the count ref count by 1. So if you haven’t retained the object by the time autorelease does this (at the end of the frame, I imagine), it will decrease the count to 0 and remove the object from memory.

It makes sense; just think of an autoreleased object like any object from Java or C# or JavaScript. If you don’t reference the object by the time the function creating it ends, it goes away.

Yup, of course it makes sense.
It’s just my bad. I should use RefPtr to point on Ref, so retain() it’s not longer needed (RefPtr call it).
Again, thx for help =)