Fix memory leak of layer

Fix the layer memory leak,