What's wrong calling layer->release() before scene->addChild(layer) ?

This code works fine:
cocos2d::CCScene* Box2DTestLayer::scene() {
CCScene scene = CCScene::create;
// ‘layer’ is an autorelease object
CCLayer
layer = new Box2DTestLayer();

// add layer as a child to scene
scene~~>addChild;
layer~~>release();
return scene;
}

If layer~~>release called before scene~~>addChild(layer) as following, the layer just could not catch any touch events:

cocos2d::CCScene* Box2DTestLayer::scene() {

CCScene scene = CCScene::create;
// ‘layer’ is an autorelease object
CCLayer
layer = new Box2DTestLayer()::create();

// add layer as a child to scene
scene~~>addChild;
layer~~>release();
return scene;
}

/////////////////////////////////////////////////////////////////////////////////
I run the Box2dTest in this way:

// create a scene. it’s an autorelease object
CCScene* scene = Box2DTestLayer::scene();
pDirector->runWithScene(scene);

Please read [[Memory Management in Cocos2d-x]].

In few words, whenever you call release on a newly created object before using it your object gets immediatelly destroyed.

Igor Zavorotkin wrote:

Please read [[Memory Management in Cocos2d-x]].
>
In few words, whenever you call release on a newly created object before using it your object gets immediatelly destroyed.

Thanks for your reply! But if the layer destroyed before added to the scene, what about the scene? It seems that they are created the same way.
I found that I had pasted a duplicate line " layer~~>release; ":
>>> will not work

CCLayer *layer = Box2DTestLayer::create;
scene~~>addChild(layer);

>>> worked

CCLayer *layer = new Box2DTestLayer();
scene->addChild(layer);
layer.release(); // or should call layer.autorelease(); ?